2

嗨,我正在使用 python mechanize 从网页中获取数据。我正在尝试从谷歌图片搜索网页获取 imgurl 以下载搜索结果图片。

这是我的代码,我将搜索表单填写为“狗”并提交。(搜索“狗”)

import mechanize
import cookielib
import urllib2
import urllib

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (x11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'), ('Accept', '*/*') ,('Accept-Language', 'ko-KR')]

br.open('http://www.google.com/imghp?hl=en')
br.select_form(nr=0)
br.form['q'] = 'dog'
a = br.submit()
searched_url = br.geturl()

file0 = open("1.html", "wb")
file0.write(a.read())
file0.close()

当我从 chrome 浏览器看到页面源时,页面源中有“imgurl”。但是当我从 python mechanize 读取数据时,没有这样的事情。此外,1.html(我用python编写)的大小比从chrome下载的html文件小得多。如何使用 python 获得与网络浏览器完全相同的 html 数据?

我是否必须设置与网络浏览器相同的请求标头?谢谢

4

0 回答 0