【问题标题】:How to crawl multiple URLs from csv using Selenium and Scrapy如何使用 Selenium 和 Scrapy 从 csv 抓取多个 URL
【发布时间】:2019-09-24 21:16:01
【问题描述】:

我目前正在尝试从https://blogabet.com/ 抓取多个站点 目前,我有一个“ursl.txt”文件,其中包含两个 URL:1.http://sabobic.blogabet.com 2.http://dedi22.blogabet.com

我遇到的问题如下: Selenium 在同一个选项卡中一个接一个地打开两个 URL 中的每一个。因此,它只是在我的“ursl.txt”文件两次中抓取第二个 ULR 的内容。它不会从第一个 URL 抓取任何内容。

我认为 for 循环以及“parse_tip”函数的调用方式存在问题。这是我的代码:

import scrapy
from scrapy import Spider
from scrapy.selector import Selector
from scrapy.http import Request

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

import re
import csv
from time import sleep

class AlltipsSpider(Spider):
    name = 'alltips'
    allowed_domains = ['blogabet.com']
    # We are not using the response parameter in this function because the start urls are not defined 
    # Our class Spider is searching for the function start_requests by default 
    # Request has to returned or yield 

    def start_requests(self):

        self.driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')    
        with open("urls.txt", "rt") as f:
            start_urls = [url.strip() for url in f.readlines()]
            for url in start_urls:
                self.driver.get(url) 
                self.driver.find_element_by_id('currentTab').click()
                sleep(3)
                self.logger.info('Sleeping for 5 sec.')
                self.driver.find_element_by_xpath('//*[@id="_blog-menu"]/div[2]/div/div[2]/a[3]').click()
                sleep(7)
                self.logger.info('Sleeping for 7 sec.')                           
                yield Request(url, callback=self.parse_tip)    

    def parse_tip(self, response):
        sel = Selector(text=self.driver.page_source)
        allposts = sel.xpath('//*[@class="block media _feedPick feed-pick"]')

        for post in allposts:
            username = post.xpath('.//div[@class="col-sm-7 col-lg-6 no-padding"]/a/@title').extract()
            publish_date = post.xpath('.//*[@class="bet-age text-muted"]/text()').extract()

            yield{'Username': username,
                'Publish date': publish_date
                }

【问题讨论】:

标签: selenium web-scraping scrapy


【解决方案1】:

当您已经收到来自 Selenium 的响应时,为什么还要执行另一个请求 yield Request(url, callback=self.parse_tip)。 只需将该响应文本传递给 parse_tip 并在其中使用文本

class AlltipsSpider(Spider):
    name = 'alltips'
    allowed_domains = ['blogabet.com']

    def start_requests(self):

        self.driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe')    
        with open("urls.txt", "rt") as f:
            start_urls = [url.strip() for url in f.readlines()]
            for url in start_urls:
                self.driver.get(url) 
                self.driver.find_element_by_id('currentTab').click()
                sleep(3)
                self.logger.info('Sleeping for 5 sec.')
                self.driver.find_element_by_xpath('//*[@id="_blog-menu"]/div[2]/div/div[2]/a[3]').click()
                sleep(7)
                self.logger.info('Sleeping for 7 sec.')                           
                for item in self.parse_tip(text= self.driver.page_source):
                    yield item

    def parse_tip(self, text):
        sel = Selector(text=text)
        allposts = sel.xpath('//*[@class="block media _feedPick feed-pick"]')

        for post in allposts:
            username = post.xpath('.//div[@class="col-sm-7 col-lg-6 no-padding"]/a/@title').extract()
            publish_date = post.xpath('.//*[@class="bet-age text-muted"]/text()').extract()

            yield{'Username': username,
                'Publish date': publish_date
                }

【讨论】:

  • 你尝试运行代码了吗?我收到一个错误for item in yield self.parse_tip(text=self.driver.page_source): ^ SyntaxError: invalid syntax
  • 我没有运行代码,你应该注意语法错误,我是来告诉逻辑的,反正应该是for item in self.parse_tip(text=self.driver.page_source): ... ,,, 我更新了代码我的回答也是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2019-01-25
  • 2018-04-15
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多