【发布时间】:2021-01-04 20:04:25
【问题描述】:
我无法确定如何在该网站上跟踪分页(检查 start_urls)。它所做的是打开 webdriver,成功从第一页抓取数据,然后在加载第二页时关闭 webdriver,仅此而已。
import scrapy
from lxml.html import fromstring
from ..items import PontsItems
from selenium import webdriver
class Names(scrapy.Spider):
name = 'enseafr'
download_delay = 5.0
start_urls = ['https://www.ponts.org/fr/annuaire/recherche?result=1&annuaire_mode=standard&annuaire_as_no=&keyword=&PersonneNom=&PersonnePrenom=&DiplomePromo%5B%5D=2023&DiplomePromo%5B%5D=2022&DiplomePromo%5B%5D=2021&DiplomePromo%5B%5D=2020&DiplomePromo%5B%5D=2019&DiplomePromo%5B%5D=2018&DiplomePromo%5B%5D=2017&DiplomePromo%5B%5D=2016&DiplomePromo%5B%5D=2015&DiplomePromo%5B%5D=2014&DiplomePromo%5B%5D=2013&DiplomePromo%5B%5D=2012&DiplomePromo%5B%5D=2011&DiplomePromo%5B%5D=2010']
def __init__(self):
self.driver = webdriver.Chrome()
def parse(self, response):
items = PontsItems()
self.driver.get(response.url)
next = self.driver.find_element_by_xpath('//a[@class="next"]')
#'//*[@id="zoneAnnuaire_layout"]/div[3]/div[2]/div[3]/div[11]/a[4]'
while True:
try:
next.click()
for item in response.xpath('//div[@class="single_desc"]'):
name = item.xpath('./div[@class="single_libel"]/a/text()').get().strip()
description = item.xpath('./div[@class="single_details"]/div/text()').get()
description = fromstring(description).text_content().strip()
year = item.xpath('./div[@class="single_details"]/div/b/text()').get()
items['name'] = name
items['description'] = description
items['year'] = year
yield items
except:
break
self.driver.close()
这几天我真的被这件事困住了。
【问题讨论】:
-
您好,请问是什么问题?
-
我得到这个:selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档(会话信息:chrome=85.0.4183.102)
-
如果您使用
Chrome来点击项目,那么您应该在self.driver.page_source中搜索而不是response,或者干脆使用self.driver.find_element_by_xpath而不是response.xpath来搜索值。 -
当我使用 self.driver.find_element_by_xpath 我得到: TypeError: 'WebElement' object is not iterable 更不用说像 [984:16892:0918/012923.411:ERROR:device_event_log_impl.cc 这样全新的东西了(208)] [01:29:23.411] 蓝牙:bluetooth_adapter_winrt.cc:1074 获取默认适配器失败。 O_o 你们能向我解释一下这个魔法,或者更好地重写这个脚本以在某种程度上实际工作吗?
-
先把next放到try catch里面。
标签: python selenium pagination scrapy