April_Leon 2019-11-22 10:17 采纳率: 0%
浏览 512
已结题

Scrapy爬取谷歌应用市场

我这样写逻辑有错误吗?为什么在parse_search函数里取不到href的值呢?

# -*- coding: utf-8 -*-
import scrapy

from GP_Spider.items import GpItem
from scrapy import Request


class GoogleSpider(scrapy.Spider):
    name = 'google'
    allowed_domains = ['google.play.com']
    start_urls = ['https://play.google.com/store']

    def parse(self, response):
        keywords = [
            'stuttering', 'speech%20therapy', 'speech%20and%20language%20therapy', 'aphasia', 'apraxia', 'dysarthria'
        ]

        link_flag = 0

        urls = []
        for each in keywords:
            app_url = ("https://play.google.com/store/search?q=" + keywords[link_flag] + '&c=apps')
            print(app_url)
            yield Request(url=app_url, callback=self.parse_search, dont_filter=True)
            link_flag += 1

    def parse_search(self, response):
        print("START PARSING")
        selector = scrapy.Selector(response)
        #print(response.body)

        urls = selector.xpath('//a[@class="poRVub" and aria-hidden="true"]/@href').extract()
        #urls = selector.xpath('//*[@id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/c-wiz/c-wiz/c-wiz/div/div[2]/div[1]/c-wiz/div/div/div[1]/div/div/a/@href').extract()
        print(urls)

        link_flag = 0
        links = []
        for link in urls:
            links.append(link)

        for each in urls:
            yield Request(url="https://play.google.com" + links[link_flag], callback=self.parse_detail, dont_filter=True)
            print("https://play.google.com" + links[link_flag])
            link_flag += 1

    def parse_detail(self, response):
        item = GpItem()
        item['app_url'] = response.url
        item['app_name'] = response.xpath('//h1[@itemprop="name"]/span').xpath('text()').get()
        item['app_icon'] = response.xpath('//img[@itemprop="image"]/@src').get()
        item['app_rate'] = response.xpath('//div[@class="K9wGie"]/div[@class="BHMmbe"]').xpath('text()').get()
        item['app_version'] = response.xpath('//div[@class="IQ1z0d"]/span[@class="htlgb"]').xpath('text()').get()
        item['app_description'] = response.xpath('//div[@itemprop="description"]/span/div').xpath('text()').get()
        # item['app_developer'] = response.xpath('//')
        # print(response.text)
        yield item

这个xpath路径是我自己写的,如果直接从chrome浏览器复制下来的话,就可以爬到特定的那个搜索结果页面的url,但是其他搜索结果页就爬不到,这是为什么?
求教各位大佬

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-11-22 10:26
    关注

    抓包检查对照下各个参数,特别是referer user-agent cookie,postdata的内容,url地址上的参数。

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?