我希望获取所有链接以及start_time和end_time一页,然后发送到函数(parse_detail)以废弃其他信息 但我不知道如何使用硒来循环
这是我的代码 并且有错误:
for site in sites:
exceptions.TypeError: 'WebElement' object is not iterable
请教我如何在硒中使用环状scrapy。谢谢!
class ProductSpider(Spider):
name = "city20140808"
start_urls = ['http://wwwtt.tw/11']
def __init__(self):
self.driver = webdriver.Firefox()
dispatcher.connect(self.spider_closed, signals.spider_closed)
def parse(self, response):
self.driver.get(response.url)
item = CitytalkItem()
sites = self.driver.find_element_by_css_selector("div.body ")
for site in sites:
linkiwant = site.find_element_by_css_selector(".heading a")
start = site.find_element_by_css_selector("div.content p.m span.date")
end = site.find_element_by_css_selector("div.content p.m span.date")
item['link'] = linkiwant.get_attribute("href")
item['start_date'] = start.text
item['end_date'] = end.text
yield Request(url=item['link'], meta={'items':items}, callback=self.parse_detail)
def parse_detail(self,response):
item = response.meta['items']
........
yield item
答案 0 :(得分:5)
而不是返回单个元素的find_element_by_css_selector()
,您需要使用find_elements_by_css_selector()
,它返回元素列表。