我正在尝试使用 scrapy 从网页中抓取产品信息。我要抓取的网页如下所示:
- 从包含 10 个产品的 Product_list 页面开始
- 单击“下一步”按钮将加载接下来的 10 个产品(两个页面之间的 URL 不会改变)
- 我使用 LinkExtractor 跟踪每个产品链接进入产品页面,并获取我需要的所有信息
我尝试复制 next-button-ajax-call 但无法工作,所以我尝试使用 selenium。我可以在单独的脚本中运行selenium的webdriver,但我不知道如何与scrapy集成。我应该把硒部分放在我的 scrapy 蜘蛛中哪里?
我的蜘蛛非常标准,如下所示:
class ProductSpider(CrawlSpider):
name = "product_spider"
allowed_domains = ['example.com']
start_urls = ['http://example.com/shanghai']
rules = [
Rule(SgmlLinkExtractor(restrict_xpaths='//div[@id="productList"]//dl[@class="t2"]//dt'), callback='parse_product'),
]
def parse_product(self, response):
self.log("parsing product %s" %response.url, level=INFO)
hxs = HtmlXPathSelector(response)
# actual data follows
任何想法表示赞赏。谢谢你!
这实际上取决于您需要如何抓取网站以及您想要获取什么数据以及如何获取数据。
以下是如何使用以下命令跟踪 eBay 上的分页的示例Scrapy
+Selenium
:
import scrapy
from selenium import webdriver
class ProductSpider(scrapy.Spider):
name = "product_spider"
allowed_domains = ['ebay.com']
start_urls = ['http://www.ebay.com/sch/i.html?_odkw=books&_osacat=0&_trksid=p2045573.m570.l1313.TR0.TRC0.Xpython&_nkw=python&_sacat=0&_from=R40']
def __init__(self):
self.driver = webdriver.Firefox()
def parse(self, response):
self.driver.get(response.url)
while True:
next = self.driver.find_element_by_xpath('//td[@class="pagn-next"]/a')
try:
next.click()
# get the data and write it to scrapy items
except:
break
self.driver.close()
以下是“硒蜘蛛”的一些示例:
- 在python中使用scrapy执行Javascript提交表单函数 https://stackoverflow.com/questions/10648644/executing-javascript-submit-form-functions-using-scrapy-in-python
- https://gist.github.com/cheekybastard/4944914 https://gist.github.com/cheekybastard/4944914
- https://gist.github.com/irfani/1045108 https://gist.github.com/irfani/1045108
- http://snipplr.com/view/66998/ http://snipplr.com/view/66998/
还有一种替代方法必须使用Selenium
with Scrapy
。在某些情况下,使用ScrapyJS中间件 https://github.com/scrapinghub/scrapy-splash足以处理页面的动态部分。实际使用示例:
- 使用 python-Scrapy 抓取动态内容 https://stackoverflow.com/questions/30345623/scraping-dynamic-content-using-python-scrapy
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)