Search code examples
web-scrapingscrapyscrapy-shell

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


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

twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion.>]

I found out that this can happen, when no user agent is set. But after setting it manually, I still got the same error.

You can see the whole output of scrapy shell here: http://pastebin.com/ZFJZ2UXe

Notes:

I am not behind a proxy, and I can access other sites via scrapy shell without problems. I am also able to access the site with Chrome, so it is not a network or connection issue.

Maybe someone can give me a hint how I could solve this problem?


Solution

  • 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

    enter image description here