【发布时间】:2017-08-17 08:34:55
【问题描述】:
我一直致力于使用 selenium 和 scrapy 抓取 this 网站。我希望我的代码点击每个公司链接,然后提取并循环这个过程。但我不知道如何从一个公司链接转到另一个。
任何帮助将不胜感激。
from scrapy.http import TextResponse
from selenium import webdriver
import scrapy
import time
class ExampleSpider(scrapy.Spider):
name = 'comp'
allowed_domains = ['site']
start_urls = ["site"]
def __init__(self, **kwargs):
super(ExampleSpider, self).__init__(**kwargs)
self.driver = webdriver.Firefox()
def parse(self, response):
self.driver.get(response.url)
self.driver.implicitly_wait(10)
index = 0
while True:
companies = self.driver.find_elements_by_xpath('//*[@id="company-list"]/ul/li')
try:
companies[index].click()
time.sleep(6)
except IndexError:
break
resp = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8')
for com in resp.xpath('body'):
yield \
{
# DO Something
}
self.driver.back()
index += 1
self.driver.quit()
它只从第一个链接中提取然后停止。请帮帮我。
【问题讨论】:
-
你坚持使用 Selenium 吗?此页面似乎正在使用 API - 尝试在浏览器的开发人员工具中查找 XHR 请求。
标签: python loops selenium xpath scrapy