【问题标题】:raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbiddenraise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden
【发布时间】:2018-11-01 15:59:55
【问题描述】:

漫画(日本漫画)没有 pdf 格式,但有原始图像,所以我创建了一个可以自动下载照片的工具。

这是我的代码

import requests
import urllib.request
from bs4 import BeautifulSoup

responsePage = input("Enter URL: ")
response = requests.get(responsePage)

soup = BeautifulSoup(response.text, 'html.parser')
images = soup.findAll('img')

image_name = 0

for img in images[1:-1]:
    image_name += 1
    url = img['src']
    full_name = str(image_name) + '.jpg'
    print(response.headers)
    resource_image = urllib.request.urlretrieve(url, full_name)
    print(full_name + " Saved!")

我收到的错误是:

urllib.error.HTTPError: HTTP Error 403: Forbidden

关于如何解决这个问题的任何建议? 帮助将不胜感激。

【问题讨论】:

  • 这是禁止的错误,表示您输入的网址需要认证,只有您自己授权才能访问它
  • 那么,我能解决这个问题吗?
  • 您需要编写代码以使用 url 支持的任何方法来授权自己,您没有提到任何 url,所以我只能建议您检查自己如何使用令牌/会话或凭据进行授权
  • 嗯,网址是

标签: python urllib


【解决方案1】:
import requests
from bs4 import BeautifulSoup
import os

def download_image(url, path):
    r = requests.get(url, stream=True)
    if r.status_code == 200:
        with open(path, 'wb') as f:
            for chunk in r:
                f.write(chunk)

URL = "https://manganelo.com/chapter/read_detective_conan_manga_online_free/chapter_10"
r = requests.get(URL)

soup = BeautifulSoup(r.text, 'html.parser')
images = soup.findAll('img')
for i in images:
    url = i.attrs["src"]
    os.makedirs(url.split('/')[-2], exist_ok=True)
    download_image(url, os.path.join(url.split('/')[-2], url.split('/')[-1]))

【讨论】:

    猜你喜欢
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2021-04-02
    • 2021-01-24
    • 2022-12-19
    • 2022-12-04
    • 2021-12-08
    相关资源
    最近更新 更多