【问题标题】:How to get around 403 error for wget download如何解决 wget 下载的 403 错误
【发布时间】:2019-11-26 23:18:36
【问题描述】:

现代的爬虫能否推荐如何使用 scrapy 解决 403 错误?

我尝试过使用 Selenium,但无济于事。我还尝试将以下标头传递给每个响应: headers= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0'}

# -*- coding: utf-8 -*-
import scrapy
import re
import os
import wget

class LivelinguaCrawlerSpider(scrapy.Spider):
    name = 'livelingua_crawler'
    allowed_domains = ['www.livelingua.com']
    start_urls = ['https://www.livelingua.com/project/']

    def parse(self, response):
        language_links = response.css("div.col-md-4 a::attr(href)").getall()
        for link in language_links[2:4]:
            language = re.match('(.*)(?<=courses)(.*)', link).group(2)[1:-1]
            dir_path = "redacted/" + language
            try:
                os.makedirs(dir_path)
            except FileExistsError:
                pass
            self.dir_path = dir_path
            request = response.follow(link, self.parseCourses)
            request.meta['dir'] = dir_path
            yield request

    def parseCourses(self, response):
        courses = response.css("span.thumb-info-caption h6 a::attr(href)").getall()
        dir_path = response.meta['dir']
        for course in courses:
            request = response.follow(course, self.parseEBooks)
            request.meta['dir'] = dir_path
            yield request

    def parseEBooks(self, response):
        eBooks = response.css("div.row:first-child ul li a::attr(href)").getall()
        for eBook in eBooks:
            wget.download(eBook, response.meta['dir'])
            yield {"ebook": eBook}

我在尝试 wget 时收到以下错误:

wget.download(eBook, response.meta['dir'])
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wget.py", line 526, in download
    (tmpfile, headers) = ulib.urlretrieve(binurl, tmpfile, callback)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

【问题讨论】:

  • 为什么要使用wget? Scrapy 媒体管道呢?
  • 403 表示服务器不允许访问该 URL。是什么让您认为另一个客户会改变这一点?如果您知道某些客户端可以访问相同的 URL 而不会收到 403,请尝试捕获包含所有标头的确切 HTTP 请求。然后将每个标头字段与您在收到 403 响应时发送的内容进行比较。

标签: python web-scraping scrapy urllib http-status-code-403


【解决方案1】:

正如 gangabas 所建议的,使用文件管道是这里的方法。 这很容易做到:

在settings.py中添加文件管道并设置包含要下载的url的字段:

ITEM_PIPELINES = {'scrapy.pipelines.files.FilesPipeline': 1}
FILES_URLS_FIELD = 'ebook'

您当前尝试下载时遇到的错误代码可能与 Roland Weber 所说的标题有关。为了解决这个问题,我建议创建一个继承自 FilesPipeline 并覆盖 get_media_requests 方法的类:

class DownloadEbooksPipeline(FilesPipeline):
    headers = {
      'Connection': 'keep-alive',
      'Upgrade-Insecure-Requests': '1',
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
      'Accept-Encoding': 'gzip, deflate, br',
      'Accept-Language': 'en-GB,en;q=0.9,nl-BE;q=0.8,nl;q=0.7,ro-RO;q=0.6,ro;q=0.5,en-US;q=0.4',
    }

    def get_media_requests(self, item, info):
        for ebook_url in item.get(self.files_urls_field, []):
            request = Request(url=ebook_url,
                              headers=self.headers)
            yield request

如果你这样做,你必须更改 settings.py 中的 ITEM_PIPELINES 以引用包含这个类的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2020-01-29
    • 2016-06-19
    • 2019-12-27
    相关资源
    最近更新 更多