【问题标题】:How to get around being blocked with "Scrapy"如何绕过被“Scrapy”阻止
【发布时间】:2020-04-27 15:07:23
【问题描述】:

背景:

我正计划购买汽车,并想监控价格。 我想使用Scrapy 为我做这件事。但是该网站阻止了我的代码执行此操作。

MWE/代码:

#!/usr/bin/python3

# from bs4 import BeautifulSoup
import scrapy    # adding scrapy to our file

urls = ['https://www.carsales.com.au/cars/volkswagen/golf/7-series/wagon-bodystyle/diesel-fueltype/']

class HeadphoneSpider(scrapy.Spider):   # our class inherits from scrapy.Spider
    name = "headphones"
    def start_requests(self):
        urls = ['https://www.carsales.com.au/cars/volkswagen/golf/7-series/wagon-bodystyle/diesel-fueltype/']# list to enter our urls
        # urls = ['https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=headphones&rh=i%3Aaps%2Ck%3Aheadphones&ajr=2']

        for url in urls:            
            yield scrapy.Request(url=url, callback=self.parse)  # we will explain the callback soon


    def parse(self, response):
        img_urls = response.css('img::attr(src)').extract()
        with open('urls.txt', 'w') as f:
            for u in img_urls:
                f.write(u + "\n")


    def main():
        scraper()

输出:

   ...some stuff above it
   2020-01-10 00:37:59 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <403 https://www.carsales.com.au/cars/volkswagen/golf/7-series/wagon-bodystyle/diesel-fueltype/>: HTTP status code is not handled or not allowed
   ..some more stuff underneath

问题:

我只是不知道如何绕过这个not allowed 来解析价格、公里数等。这会让我的生活变得更轻松。我怎样才能越过这个街区? FWIW 我也用 BeautifulSoup 进行了尝试,但没有成功。

【问题讨论】:

  • 这能回答你的问题吗? Scraping in Python - Preventing IP ban
  • @ggorlen 我应该使用 Scrapy 吗?似乎是 BeautifulSoup 之后的另一个层次 - 帮助!~?
  • 我不知道,但是网上有很多关于这个问题的问题和文章,所以我认为你需要在这个问题出现之前进行更多的研究获得有用的关注,因为有各种各样的通用技术(在欺骗和其他线程中描述)可以帮助你。
  • @ggorlen 我正在尝试使用 Scrapy,但我得到了not allowed 卡...:/

标签: scrapy


【解决方案1】:

有多种方法可以避免在抓取时被网站阻止:

  • 设置ROBOTSTXT_OBEY = False
  • 在您的请求之间增加 DOWNLOAD_DELAY,例如 3 到 4 秒,具体取决于站点行为
  • CONCURRENT_REQUESTS 设置为1
  • 通过自定义 proxy_middleware 使用代理或代理池并服务于原因
  • 在请求中携带站点 cookie,以便站点无法识别机器人行为

您可以依次尝试这些解决方案

【讨论】:

  • 我会在哪里写这些命令?你能提供一个sn-p的代码吗?我是scrapy的新手
  • 您必须将这些写入设置文件中。请参考 Scrapy 设置文档,它将让您清楚地了解如何使用这些设置 [docs.scrapy.org/en/latest/topics/settings.html]
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多