我正在写一个带有硒的scrapy蜘蛛来制作动态网页。 我很确定正则表达式工作正常。但是' page_link'来自linkextractor没有任何东西,程序在调用parse_item函数之前终止。无法弄清楚出了什么问题。
class OikotieSpider(CrawlSpider):
name = 'oikotie'
allowed_domains = [my_domain]
start_urls=['https://asunnot.oikotie.fi/myytavat-uudisasunnot?cardType=100&locations=%5B%22helsinki%22%5D&newDevelopment=1&buildingType%5B%5D=1&buildingType%5B%5D=256&pagination=1']
def __init__(self):
CrawlSpider.__init__(self)
chrome_driver = 'mydriver_location'
os.environ["webdriver.chrome.driver"] = chrome_driver
chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chromeOptions.add_experimental_option("prefs", prefs)
#driver instance and call
self.driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chromeOptions)
self.driver.get('my_url')
self.selector=Selector(text=self.driver.page_source)
self.driver.close()
self.driver.quit()
page_link=LinkExtractor(allow=('myytavat-asunnot\/helsinki\/\d+',))
rules = (
# Extract links matching 'item.php' and parse them with the spider's method parse_item
Rule(page_link, callback='parse_item',follow=True),
)
def parse_item(self, response):
self.logger.info('Hi, this is an item page! %s', response.url)
print("parse_item is called!!")
self.driver.get(response.url)
self.driver.implicitly_wait(30)
return ....
答案 0 :(得分:0)
我认为你应该使用' DownloadMiddleware'实现获取网页。在“下载中心”中,您可以初始化浏览器以获取网页。看这个: https://doc.scrapy.org/en/latest/topics/downloader-middleware.html?highlight=DownloadMiddleware
答案 1 :(得分:0)
看起来你的LinkExtractor allow
参数不是绝对的正则表达式。它必须是:https://doc.scrapy.org/en/latest/topics/link-extractors.html。
现在作为绝对正则表达式的一部分可能只是* +当前的正则表达式......但那将是可怕的:)。只是让它绝对。