【问题标题】:Using Scrapy 1.6.0 got DEBUG: Crawled (200)使用 Scrapy 1.6.0 得到了 DEBUG: Crawled (200)
【发布时间】:2019-11-18 00:04:24
【问题描述】:

我是scrapy的新手,尝试从不同的页面抓取。

以下基于 Selenium。它会在 100 页后粉碎,速度为每秒 1 页。我认为这可能是网站的限制。为避免这种情况,禁用 cookie 或使用虚假标头可能会起作用。我到现在都没试过。我真的需要一些很好的建议。非常感谢!

class test1113(scrapy.Spider):
    name = "chrome"

    def __init__(self):
        chromeOptions = webdriver.ChromeOptions()
        prefs = {"profile.managed_default_content_settings.images": 2}
        chromeOptions.add_experimental_option("prefs", prefs)
        self.driver = webdriver.Chrome('D:\chrome\chromedriver.exe', chrome_options=chromeOptions)
        # self.driver = webdriver.Chrome('D:\chrome\chromedriver.exe')

    def start_requests(self):
        urls = [
            'https://www.emsc-csem.org/Earthquake/?view=1'
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        self.driver.get(response.url)

        while True:
            next_page = self.driver.find_elements_by_css_selector('*[alt="Next page"]')[0]
            try:
                tables = response.xpath('//table')
                table = tables[3]
                rows = table.xpath('//tr[@id]')
                for row in rows:
                    temp = row.xpath('td//text()').extract()
                    if (12 == len(temp) and temp[0] == 'earthquake'):
                        yield {
                            'time:': ' '.join(temp[1].split()),
                            'latitude:': ' '.join((temp[3] + temp[4]).split()),
                            'longitude:': ' '.join((temp[5] + temp[6]).split()),
                            'depth:': ' '.join(temp[7].split()),
                            'magnitude:': ' '.join(temp[9].split()),
                            'region:': ' '.join(temp[10].split()),
                            'update time:': ' '.join(temp[11].split()),
                        }
                        print('time:', ' '.join(temp[1].split()))
                next_page.click()
            except:
                break
        self.driver.close()

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    嗯,这不是错误,Scrapy 确实运行良好。

    我在这里看到的主要问题是该网页上的内容需要一些时间来加载,因此您的脚本需要等待,您可以通过使用 Selenium 或 Splash 来实现。

    我的建议是使用 Splash,它是为与 Scrapy 一起使用而开发的,所以这里有一些可以帮助你的线索。

    Scrapy-Splash 的官方 GitHub 仓库: https://github.com/scrapy-plugins/scrapy-splash

    文档:https://splash.readthedocs.io/en/stable/faq.html

    下面是 Splash 与 Scrapy 集成的示例:

    https://blog.scrapinghub.com/2015/03/02/handling-javascript-in-scrapy-with-splash

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回答。由于我的系统限制,我不能使用 docker,所以我尝试了 Selenium。不过,速度真的很慢,像每秒1页的东西。并且程序会在 100 多页后粉碎。我想也许我需要使用假标题并禁用 cookie 以避免网站限制。直到现在,我都无法实现我的理想。那么,您能给我更多的建议吗?非常感谢!
    • 好吧,关于 Selenium 的速度不要期望太多,它是为测试目的而构建的,而不是为了网络抓取。对于崩溃,它们主要是因为 Selenium 有一些内部错误,所以你需要更多地研究如何克服它们。但是如果你想保持低并且不被阻止,你将需要使用一些代理,我建议许多 Scrapy 开发人员使用 ScrapingHub,你可以使用他们的 Crawlera,这样你就可以在他们的服务器上托管你的蜘蛛,他们会做剩下的!
    • PS:通过 ScrapingHub 的 Crawlera 是付费服务,因此,如果您建立的业务很大程度上依赖于您的数据提取,则不会出错。如果你是出于学习目的,ScrapingHub 包含在 GitHub 学生包中,因此你可以在他们的服务器上免费托管你的 Scrapy Spider,但你的爬取时间和数据存储有限。
    • 如果您认为我的回答帮助您解决了您的问题,请选择它作为此问题的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多