简体   繁体   English

带有 HTTP 错误 400:错误请求的 Python 脚本

[英]Python Script with HTTP Error 400: Bad Request

This code is to get some images from the URL stored in the excel file.此代码是从存储在excel文件中的URL中获取一些图像。 The code is also to save them but when I ran the code, I got this error "HTTPError: HTTP Error 400: Bad Request".代码也是为了保存它们,但是当我运行代码时,出现了这个错误“HTTPError: HTTP Error 400: Bad Request”。 What can I do to solve the problem?我能做些什么来解决问题?

import pandas as pd
import urllib.request

def url_to_jpg(i, url, file_Path):

    filename = 'image_{}.jpg'.format(i)
    full_path = '{}{}'.format(file_Path, filename)
    urllib.request.urlretrieve(url, full_path)

    print('{} saved.'.format(filename))
    return None

File_Name = 'Cooler_URL_2.xlsx'
File_Path = 'images/'

#reading the file as pandas dataframe
urls = pd.read_excel(File_Name)

#save file as directory
for i, url in enumerate(url_values):
    url_to_jpg(i, url[0], File_Path)

As mentioned, the most common cause of a 400 Bad Request Error is simply inputting an incorrect URL.如前所述,400 错误请求错误的最常见原因只是输入了错误的 URL。 Domain names (eg abc.com) are case-insensitive, meaning that this mixed case link to ABC.COM works just as well as the normal, lowercase version of abc.com.域名(例如 abc.com)不区分大小写,这意味着这种到 ABC.COM 的混合大小写链接与 abc.com 的正常小写版本一样有效。

下面这行for i, url in enumerate(url_values): url_to_jpg(i, url[0], File_Path)被替换为for i, url in enumerate(url_values): url_to_jpg(i, url, File_Path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM