python - Scrapy spider 没有显示完整的结果

标签 python web-scraping scrapy

大家好,我正在尝试从代码中的给定链接获取完整结果。但我的代码没有给出所有结果。这个链接说它包含 2132 个结果,但它只返回 20 个结果。:

from scrapy.spider import Spider
from scrapy.selector import Selector
from tutorial.items import Flipkart
 class Test(Spider):
   name = "flip"
   allowed_domains = ["flipkart.com"]
   start_urls = ["http://www.flipkart.com/mobiles/pr?sid=tyy,4io&  otracker=ch_vn_mobile_filter_Mobile%20Brands_All"
]
  def parse(self, response):
     sel = Selector(response)
     sites = sel.xpath('//div[@class="pu-details lastUnit"]')
     items = []
     for site in sites:
         item = Flipkart()
         item['title'] = site.xpath('div[1]/a/text()').extract()
         items.append(item)
     return items**

最佳答案

这是因为该网站一次只显示 20 个结果,当用户滚动到页面底部时,会使用 JavaScript 加载更多结果。

这里有两个选择:

  • 在站点上找到一个链接,该链接在单个页面上显示所有结果(怀疑它是否存在,但是某些站点可能会在传递可选查询字符串时这样做)。
  • 在您的蜘蛛程序中处理 JavaScript 事件。默认的 Scrapy 下载器不会这样做,所以你可以分析 JS 代码并自己以编程方式发送事件信号,或者使用类似 Selenium 的东西。带 PhantomJS让浏览器处理它。我推荐后者,因为它比您自己解释 JS 的手动方法更不易出错。参见 this question了解更多信息,Google 周围有很多关于此主题的信息。

关于python - Scrapy spider 没有显示完整的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966367/

相关文章:

python - Xpath正确但Scrapy不起作用

unit-testing - 测试 scrapy 蜘蛛仍然工作 - 查找页面更改

python - 从python中的基类继承namedtuple

python - 检查数字零时的多个条件声明 - python

python - 在 Python 中变换矩形

python - 高效计算二重积分

javascript - Android - 使用 JSOUP 解析 JS 生成的 url

python - 是否需要为他们定位的每个站点编写爬虫?

xpath - 在 Scrapy 中选择下一个 sibling 的序列

python - 在循环中查找漂亮的汤返回 TypeError