Z. *_*Lin 74 python selenium scrapy web-scraping selenium-webdriver
我正在尝试使用scrapy从网页上抓取产品信息.我的待删节网页如下所示:
我试图复制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
Run Code Online (Sandbox Code Playgroud)
任何想法都表示赞赏.谢谢!
ale*_*cxe 104
这实际上取决于您如何刮取网站以及您希望获得的数据和数据.
这是一个如何使用Scrapy+ 在ebay上跟踪分页的示例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()
Run Code Online (Sandbox Code Playgroud)
以下是"硒蜘蛛"的一些例子:
还有一种具有使用替代的Selenium带Scrapy.在某些情况下,使用ScrapyJS中间件足以处理页面的动态部分.实际使用示例:
| 归档时间: |
|
| 查看次数: |
70442 次 |
| 最近记录: |