Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 仅当链接为';这是python中的图像_Python 3.x_Python Requests - Fatal编程技术网

Python 3.x 仅当链接为';这是python中的图像

Python 3.x 仅当链接为';这是python中的图像,python-3.x,python-requests,Python 3.x,Python Requests,我尝试了各种方法,但找不到合适的方法。在我当前的带有请求的代码中,即使是文本,它也会下载所有内容。在响应标题中,您将获得您正在获取的文件类型。 例如:对于html:text/html,对于image:image/png等。。您可以通过设置条件进行过滤 试试这个: import requests response =requests.get("https://www.google.com/imgres?imgurl=https%3A%2F%2Fupload.wikimedia.org%

我尝试了各种方法,但找不到合适的方法。在我当前的带有请求的代码中,即使是文本,它也会下载所有内容。

在响应标题中,您将获得您正在获取的文件类型。 例如:对于html:text/html,对于image:image/png等。。您可以通过设置条件进行过滤

试试这个:

import requests

response =requests.get("https://www.google.com/imgres?imgurl=https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fb%2Fb6%2FImage_created_with_a_mobile_phone.png%2F1200px-Image_created_with_a_mobile_phone.png&imgrefurl=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FImage&tbnid=gxFxsvFBmxeZ9M&vet=12ahUKEwjAm6y6qYvrAhWVTnwKHRQVDAcQMygAegUIARDNAQ..i&docid=0JWe7yDOKrVFAM&w=1200&h=900&q=image&ved=2ahUKEwjAm6y6qYvrAhWVTnwKHRQVDAcQMygAegUIARDNAQ")

if response.headers["Content-Type"] =="text/html":
    pass
else:
    print(response.text)

在响应头中,您将获得所获得的文件类型。 例如:对于html:text/html,对于image:image/png等。。您可以通过设置条件进行过滤

试试这个:

import requests

response =requests.get("https://www.google.com/imgres?imgurl=https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2Fb%2Fb6%2FImage_created_with_a_mobile_phone.png%2F1200px-Image_created_with_a_mobile_phone.png&imgrefurl=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FImage&tbnid=gxFxsvFBmxeZ9M&vet=12ahUKEwjAm6y6qYvrAhWVTnwKHRQVDAcQMygAegUIARDNAQ..i&docid=0JWe7yDOKrVFAM&w=1200&h=900&q=image&ved=2ahUKEwjAm6y6qYvrAhWVTnwKHRQVDAcQMygAegUIARDNAQ")

if response.headers["Content-Type"] =="text/html":
    pass
else:
    print(response.text)

您是否能够包含当前代码的外观?您是否能够包含当前代码的外观?