【发布时间】:2015-02-05 07:37:58
【问题描述】:
我正在尝试从 Flipkart.com 上抓取一些信息,为此我正在使用 Scrapy。我需要的信息适用于 Flipkart 上的每个产品。
我为我的蜘蛛使用了以下代码 from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.selector import HtmlXPathSelector
from tutorial.items import TutorialItem
class WebCrawler(CrawlSpider):
name = "flipkart"
allowed_domains = ['flipkart.com']
start_urls = ['http://www.flipkart.com/store-directory']
rules = [
Rule(LinkExtractor(allow=['/(.*?)/p/(.*?)']), 'parse_flipkart', cb_kwargs=None, follow=True),
Rule(LinkExtractor(allow=['/(.*?)/pr?(.*?)']), follow=True)
]
@staticmethod
def parse_flipkart(response):
hxs = HtmlXPathSelector(response)
item = FlipkartItem()
item['featureKey'] = hxs.select('//td[@class="specsKey"]/text()').extract()
yield item
我的意图是爬取每个产品类别页面(由第二条规则指定)并按照类别页面中的产品页面(第一条规则)从产品页面中抓取数据。
- 一个问题是我找不到控制抓取和报废的方法。
- 第二个 Flipkart 在其类别页面上使用 ajax,并在用户滚动到底部时显示更多产品。
- 我已阅读其他答案并评估硒可能有助于解决问题。但我找不到将其实施到此结构中的适当方法。
欢迎提出建议..:)
其他细节
我之前使用过类似的方法
我使用的第二条规则是
Rule(LinkExtractor(allow=['/(.?)/pr?(.?)']),'parse_category', follow=True)
@staticmethod
def parse_category(response):
hxs = HtmlXPathSelector(response)
count = hxs.select('//td[@class="no_of_items"]/text()').extract()
for page num in range(1,count,15):
ajax_url = response.url+"&start="+num+"&ajax=true"
return Request(ajax_url,callback="parse_category")
现在我对回调“parse_category”或“parse_flipkart”使用什么感到困惑
感谢您的耐心等待
【问题讨论】:
标签: python-2.7 selenium web-scraping scrapy