Scrapy Shell: twisted.internet.error.ConnectionLost although USER_AGENT is set

前端 未结 1 575
情书的邮戳
情书的邮戳 2021-01-07 05:46

When I try to scrape a certain web site (with both, spider and shell), I get the following error:

twisted.web._newclient.ResponseNeverReceived: [

        
相关标签:
1条回答
  • 2021-01-07 06:16

    Here is 100% working code.

    What you need to do is you have to send request headers as well.

    Also set ROBOTSTXT_OBEY = False in settings.py

    # -*- coding: utf-8 -*-
    import scrapy, logging
    from scrapy.http.request import Request
    
    class Test1SpiderSpider(scrapy.Spider):
        name = "test1_spider"
    
        def start_requests(self):
    
            headers = {
                "Host": "www.firmenabc.at",
                "Connection": "keep-alive",
                "Cache-Control": "max-age=0",
                "Upgrade-Insecure-Requests": "1",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                "DNT": "1",
                "Accept-Encoding": "gzip, deflate, sdch",
                "Accept-Language":"en-US,en;q=0.8"
            }
    
            yield Request(url= 'http://www.firmenabc.at/result.aspx?what=&where=Graz', callback=self.parse_detail_page, headers=headers)
    
        def parse_detail_page(self, response):
            logging.info(response.body)
    

    EDIT:

    You can see what headers to send by inspecting the URLs in Dev Tools

    0 讨论(0)
提交回复
热议问题