【问题标题】:Scrapy Ignoring Response 303 - HTTP status code is not handled or not allowedScrapy 忽略响应 303 - HTTP 状态代码未处理或不允许
【发布时间】:2019-11-27 08:35:31
【问题描述】:

我想刮掉https://m.youtube.com的评论

当我尝试抓取 https://m.youtube.com 时,首先将我重定向到 https://www.youtube.com。我已经对我的蜘蛛进行了编程,使其不服从robot.txt,禁用cookie,尝试了meta=dont_redirect。现在它没有将我重定向到https://www.youtube.com,但现在我得到响应“忽略响应 https://m.youtube.com/view_comment?v=xHkL9PU7o9k&gl=US&hl=en&client=mv-google>:HTTP 状态代码未处理或不允许”我该如何解决这个问题。

我的蜘蛛代码如下:

    import scrapy

    class CommentsSpider(scrapy.Spider):
        name = 'comments'
        allowed_domains = ['m.youtube.com']
        start_urls = [
        'https://m.youtube.com/view_comment? 
        v=xHkL9PU7o9k&gl=US&hl=en&client=mvgoogle'
        ]


def start_requests(self):
    for url in self.start_urls:
        yield scrapy.Request(url, meta = {'dont_redirect': True})

def parse(self, response):
    x = response.xpath('/html/body/div[4]/div[2]/text()').extract()
    y = 
       response.xpath('/html/body/div[4]/div[3]/div[2]/text()').extract()

    yield{'Comments': (x, y)}

'''

输出:

2019-07-18 16:07:23 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2019-07-18 16:07:24 [scrapy.core.engine] DEBUG: Crawled (303) <GET https://m.youtube.com/view_comment?v=xHkL9PU7o9k&gl=US&hl=en&client=mv-google> (referer: None)
2019-07-18 16:07:24 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <303 https://m.youtube.com/view_comment?v=xHkL9PU7o9k&gl=US&hl=en&client=mv-google>: HTTP status code is not handled or not allowed
2019-07-18 16:07:24 [scrapy.core.engine] INFO: Closing spider (finished)

【问题讨论】:

    标签: python python-3.x scrapy http-status-code-303


    【解决方案1】:

    我会尝试使用移动浏览器的用户代理字符串来避免被重定向:

    USER_AGENT='Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
    headers = {'User-Agent': USER_AGENT}
    
    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, headers=self.headers)
    

    【讨论】:

    • 感谢您的回复,但我的问题尚未解决,现在我收到此回复“连接被对方​​拒绝:10061:无法建立连接,因为目标机器主动拒绝了它..”跨度>
    • 您是否尝试按照 Sewake 下面的建议添加 handle_httpstatus_list = [303]?
    • 对我来说很好,也许他们现在正在阻止你的 IP。您是否尝试过使用代理?
    • 是的,我尝试使用代理,但现在我没有得到任何结果
    【解决方案2】:

    According to Scrapy documentation 你可以使用handle_httpstatus_list 蜘蛛属性。

    在你的情况下:

    class CommentsSpider(scrapy.Spider):
        name = 'comments'
        allowed_domains = ['m.youtube.com']
        start_urls = [
            'https://m.youtube.com/view_commentv=xHkL9PU7o9k&gl=US&hl=en&client=mvgoogle'
        ]
        handle_httpstatus_list = [303]
    

    【讨论】:

    • 感谢您的回复,但我的问题尚未解决,现在我收到此回复“连接被对方​​拒绝:10061:无法建立连接,因为目标机器主动拒绝了它..”跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多