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

假如想象 提交于 2019-12-03 22:44:15

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/42470303/scrapy-shell-twisted-internet-error-connectionlost-although-user-agent-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!