python - 我想使用 scrapy python 单击网站链接

标签 python selenium selenium-webdriver web-scraping scrapy

import scrapy
from selenium import webdriver


class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://ozhat-turkiye.com/en/brands/a',
    ]

I want to click a link

    def __init__(self):
        self.drivers = webdriver.Firefox('C:/Program Files (x86)\Mozilla Firefox')

I want to click a link

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

我在脚本中使用了编码等待,这是完全不推荐的。您应该将其替换为显式等待

关于python - 我想使用 scrapy python 单击网站链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52517882/

相关文章:

python - 修改内置函数

selenium - Selenium Remote Control 和 Selenium Server 有什么区别?

python - headless chromedriver python "uncaught tagerror"

java - 如何断言页面是否显示

python - 使用selenium python仅在html中的特定文本之后查找元素

python - 是什么导致通过交换 Eigen::Tensor 收缩中的张量产生不同的结果?

python - 导入如何在 IPython 中工作

python - 我可以通过带有 LinearRegressor 的钩子(Hook)记录训练损失吗?

javascript - 如何访问 webdriver 中添加的插件

javascript - 如何在 Webdriver JS 中设置脚本超时值?