python - 我想点击使用scrapy python的网站链接
问题描述
import scrapy
from selenium import webdriver
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://ozhat-turkiye.com/en/brands/a',
]
我想点击链接
def __init__(self):
self.drivers = webdriver.Firefox('C:/Program Files (x86)\Mozilla Firefox')
我想点击链接
def parse(self, response):
for title in response.css('div.tabledivinlineblock a.tablelink50::attr(href)').extract():
yield {'title': title,
'response': response.url
}
# i want to click this a tag
next = self.driver.find_element_by_xpath('//*[@id="maincontent_DataPager"]/a[last()]')
# follow pagination links
# for href in response.css('span#maincontent_DataPager a:last-child'):
#
# yield response.follow(href, self.parse)
next_page = response.css('span#maincontent_DataPager a:last-child::attr(href)').extract_first().strip()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
解决方案
以下脚本应该为您获取所需的项目,耗尽连接到下一页链接的所有点击。您不能在此处使用,response.follow()
因为除了单击它之外没有可关注的链接。
import time
import scrapy
from selenium import webdriver
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://ozhat-turkiye.com/en/brands/a',
]
def __init__(self):
self.driver = webdriver.Firefox()
def parse(self, response):
self.driver.get(response.url)
while True:
time.sleep(5)
for title in self.driver.find_elements_by_css_selector('div.tabledivinlineblock a.tablelink50'):
yield {'title': title.text,'response': response.url}
try:
self.driver.find_element_by_css_selector('span#maincontent_DataPager a:last-child').click()
except Exception: break
我在脚本中使用了硬编码等待,这根本不推荐。您应该将其替换为Explicit Wait
.
推荐阅读
- curl - 如何将 curl 安装到 jetbrains CLion
- python - 如何使变量资源管理器中的变量可从新文件访问
- c++ - 为什么将返回值分配给 int 时,它本身会减一?
- javascript - 将输入图像保存在 localStorage 中,然后使用 JS 检索它
- c++ - C++ cin 将浮点数转换为随机小数
- python - for 循环输出范围
- hash - 通用哈希应用程序
- node-modules - 将 MySQL 函数与 Node mysql2 库一起使用
- python - sklearn 导入导致 DLL 加载失败
- latex - 如何在乳胶中插入上对流时间导数?