Python 如何从谷歌获取图像';使用Mechanize搜索结果
我试图解析谷歌图像搜索结果的HTML,并获得图像的原始链接 到目前为止,我成功地编写了一个Python代码,使用Python的Mechanize和BeautifulSoup获取Google搜索的HTML 查看Google的搜索结果HTML源代码,我发现Google正在使用classPython 如何从谷歌获取图像';使用Mechanize搜索结果,python,beautifulsoup,mechanize,mechanize-python,Python,Beautifulsoup,Mechanize,Mechanize Python,我试图解析谷歌图像搜索结果的HTML,并获得图像的原始链接 到目前为止,我成功地编写了一个Python代码,使用Python的Mechanize和BeautifulSoup获取Google搜索的HTML 查看Google的搜索结果HTML源代码,我发现Google正在使用classrg_meta将原始图像URL的双重编码存储在一个div中,但我从Mechanize收到的HTML不包含任何此类。事实上,整个新网页是通过Mechanize返回的 我知道谷歌的图像搜索API,但我需要用这种方式解析HT
rg_meta
将原始图像URL的双重编码存储在一个div中,但我从Mechanize收到的HTML不包含任何此类。事实上,整个新网页是通过Mechanize返回的
我知道谷歌的图像搜索API,但我需要用这种方式解析HTML。我做错了什么?我可以将Mechanize屏蔽为Chrome或其他浏览器吗
这是我尝试的一个片段。它没有返回任何内容:
import urllib
import mechanize
from bs4 import BeautifulSoup
from urlparse import urlparse
search = "cars"
browser = mechanize.Browser()
browser.set_proxies({"https": "10.0.2.88:3128"})
browser.set_handle_robots(False)
browser.addheaders = [('User-agent','Mozilla')]
html = browser.open("https://www.google.co.in/search?&source=lnms&tbm=isch&sa=X&q="+search+"&oq="+search)
htmltext=html.read()
print htmltext
img_urls = []
formatted_images = []
soup = BeautifulSoup(htmltext)
#results = soup.findAll("a")
results = soup.findAll("div", { "class" : "rg_meta" })
print results
感谢您的尝试,但我必须切换到urllib2来解决此问题, 下面的代码正在解析google搜索页面的图像链接
search = search.replace(" ","%20")
site= "http://www.google.co.in/search?q="+search+"&tbm=isch&tbs=isz:l"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
QtGui.qApp.processEvents()
req = urllib2.Request(site, headers=hdr)
try:
QtGui.qApp.processEvents()
page = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e.fp.read()
QtGui.qApp.processEvents()
content = page.read()
#print content
soup = BeautifulSoup(content)
results = soup.findAll("a")
linkarray = soup.find_all(attrs={"class": "rg_meta"})
#print linkarray
refer_rl=[]
total=len(linkarray)
i=0
for divs in linkarray:
i=i+1
stri=str(divs)
refer_url=stri.split('%3B')
try:
QtGui.qApp.processEvents()
url=urllib.unquote(refer_url[2]).decode('utf8')
url=urllib.unquote(url).decode('utf8')
#os.system('wget '+url)
#f = open('links.txt', 'a')
#f.write(url+'\n')
form.textBrowser.append(url)
form.progressBar.setProperty("value", i*100/total)
time.sleep(0.05)
except:
continue
#os.system('aria2c -i links.txt -x 16')
#os.system('rm links.txt')
print "All good, you can download now"
导入机械化
br=mechanize.Browser()
br.open()
images=re.findall(“src=\”[^\“]{8240}”,br.response().read())
对于图像中的i:打印i
br.close()
您需要稍微过滤结果,并根据特定站点的HTML修改RE“您明确同意不通过任何自动方式(包括使用脚本或网络爬虫)访问(或尝试访问)任何服务…”是否回答?我是python新手。Google使用条款不允许在web搜索中使用脚本。尝试类似于
pattern.web的方法
import mechanize
br = mechanize.Browser()
br.open(<yoursitehere>)
images = re.findall("src=\"[^\"]{8,240}", br.response().read())
for i in images: print i
br.close()