在scrapy_fake_useragent和cfscrape scrapy扩展之间共享USER_AGENT

0lvr5msh  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(139)

我正在尝试创建一个scraper为cloudfare保护的网站使用cfscrapeprivoxy和tor,和scrapy_fake_useragent
我使用cfscrape python extension绕过带有scrapy的cloudfare保护,并使用scrapy_fake_useragent将随机的真实的USER_AGENT信息注入到头文件中。
如cfscrap文档所示:您必须使用相同的用户代理字符串来获取令牌并使用这些令牌发出请求,否则Cloudflare会将您标记为bot。

To collect cookie needed by `cfscrape`, i need to redefine the `start_request` function into my spider class, like this : 

    def start_requests(self):
        cf_requests = []
        for url in self.start_urls:
            token, agent = cfscrape.get_tokens(url)
            self.logger.info("agent = %s", agent)
            cf_requests.append(scrapy.Request(url=url,
                                              cookies= token,
                                              headers={'User-Agent': agent}))
        return cf_requests

我的问题是start_requests收集的user_agentscrapy_fake_useragent随机选择的user_agent不一样,如您所见:

017-01-11 12:15:08 [airports] INFO: agent = Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
2017-01-11 12:15:08 [scrapy.core.engine] INFO: Spider opened
2017-01-11 12:15:08 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2017-01-11 12:15:08 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-01-11 12:15:08 [scrapy_fake_useragent.middleware] DEBUG: Assign User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10 to Proxy http://127.0.0.1:8118

我在settings.py中定义了我的扩展,顺序如下:

RANDOM_UA_PER_PROXY = True
HTTPS_PROXY = 'http://127.0.0.1:8118'
COOKIES_ENABLED = True

DOWNLOADER_MIDDLEWARES = {
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
    'scrapy_fake_useragent.middleware.RandomUserAgentMiddleware': 400,
    'flight_project.middlewares.ProxyMiddleware': 100,
    'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware':110,
    }

我需要相同的user_agent,那么我如何将scrapy_fake_useragent随机提供的用户代理传递/获取到cfscrape扩展的start_requests方法中呢?

yx2lnoni

yx2lnoni1#

最后在scrapy_user_agent开发者的帮助下找到了答案。在settings.py中取消激活'scrapy_fake_useragent.middleware.RandomUserAgentMiddleware': 400行,然后编写以下源代码:

class AirportsSpider(scrapy.Spider):
    name = "airports"
    start_urls = ['https://www.flightradar24.com/data/airports']
    allowed_domains = ['flightradar24.com']

    ua = UserAgent()
    ...

    def start_requests(self):
        cf_requests = []
        user_agent = self.ua.random
        self.logger.info("RANDOM user_agent = %s", user_agent)
        for url in self.start_urls:
            token , agent = cfscrape.get_tokens(url,user_agent)
            self.logger.info("token = %s", token)
            self.logger.info("agent = %s", agent)

            cf_requests.append(scrapy.Request(url=url,
                                          cookies= token,
                                          headers={'User-Agent': agent}))
        return cf_requests

相关问题