Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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
来自BeautifulSoup的Html不同于浏览器';s源代码(Python3.5)_Python - Fatal编程技术网

来自BeautifulSoup的Html不同于浏览器';s源代码(Python3.5)

来自BeautifulSoup的Html不同于浏览器';s源代码(Python3.5),python,Python,我想从谷歌图像搜索中获得源代码,我在https://www.google.com/searchbyimage?&image_url=+图像链接有效。我正在使用Python,这是我尝试的: from bs4 import BeautifulSoup import requests browser = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7' headers

我想从谷歌图像搜索中获得源代码,我在
https://www.google.com/searchbyimage?&image_url=
+
图像链接
有效。我正在使用Python,这是我尝试的:

from bs4 import BeautifulSoup
import requests
browser = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':browser,}
url = ("https://www.google.com/searchbyimage?&image_url="+"http://mlm-s1-p.mlstatic.com/635657-MLM25528207389_042017-O.jpg")
page = requests.get(url, headers= headers)
soup = BeautifulSoup(page.text, "html.parser")

pretty = soup.prettify()
print(pretty)
如果我们将
查看源代码:
添加到,这与Chrome的源代码非常不同

如果你想知道,这个脚本的目的是为了找到谷歌对图像字符串的最佳猜测,在我的示例中,它是:
lemmy kilmister funko pop
,但我甚至在Beautiful Soup的html中找不到任何这个词


编辑:忘记包含库和美化组/请求

这是您的全部代码吗?只是澄清一下,因为您没有创建soup对象或请求页面

假设您已经这样做了,从您添加的链接中,回答者说这只适用于浏览器标题,这是一种奇怪的情况。我使用你的标题在curl中运行了你的代码,它将我重定向到头版,这就是为什么你找不到你的字符串。但是使用我当前的Firefox头运行它

 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0
我能找到正确的页面。请注意,您的代码以后不会因为这种情况而中断。我通过稍微编辑标题进行了测试,但有些改动(53.0版还行,5.0版还行)很紧

旋度命令

  curl "https://www.google.com/searchbyimage?&image_url=mlm-s1-p.mlstatic.com/635657-MLM25528207389_042017-O.jpg" -L -v -o file.html -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"

这都是你的密码吗?只是澄清一下,因为您没有创建soup对象或请求页面

假设您已经这样做了,从您添加的链接中,回答者说这只适用于浏览器标题,这是一种奇怪的情况。我使用你的标题在curl中运行了你的代码,它将我重定向到头版,这就是为什么你找不到你的字符串。但是使用我当前的Firefox头运行它

 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0
我能找到正确的页面。请注意,您的代码以后不会因为这种情况而中断。我通过稍微编辑标题进行了测试,但有些改动(53.0版还行,5.0版还行)很紧

旋度命令

  curl "https://www.google.com/searchbyimage?&image_url=mlm-s1-p.mlstatic.com/635657-MLM25528207389_042017-O.jpg" -L -v -o file.html -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0"

它可以用javascript创建。您可能需要使用dry scrape或selenium使其工作。它可能是用javascript创建的。您可能需要使用干刮或硒使其工作。