如何从Google查询获取网址?
大家好,我试着从谷歌获取网址,但结果返回了0个网址!
这是我的代码,出了什么问题呢?
import string, sys, time, urllib2, cookielib, re, random, threading, socket, os, time
def Search(go_inurl,maxc):
header = ['Mozilla/4.0 (compatible; MSIE 5.0; SunOS 5.10 sun4u; X11)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2pre) Gecko/20100207 Ubuntu/9.04 (jaunty) Namoroka/3.6.2pre',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser;',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)',
'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)',
'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6)',
'Microsoft Internet Explorer/4.0b1 (Windows 95)',
'Opera/8.00 (Windows NT 5.1; U; en)',
'amaya/9.51 libwww/5.4.0',
'Mozilla/4.0 (compatible; MSIE 5.0; AOL 4.0; Windows 95; c_athome)',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; ZoomSpider.net bot; .NET CLR 1.1.4322)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; QihooBot 1.0 qihoobot@qihoo.net)',
'Mozilla/4.0 (compatible; MSIE 5.0; Windows ME) Opera 5.11 [en]']
gnum=100
uRLS = []
counter = 0
while counter < int(maxc):
jar = cookielib.FileCookieJar("cookies")
query = 'q='+go_inurl
results_web = 'http://www.google.com/cse?'+'cx=011507635586417398641%3Aighy9va8vxw&ie=UTF-8&'+'&'+query+'&num='+str(gnum)+'&hl=en&lr=&ie=UTF-8&start=' + repr(counter) + '&sa=N'
request_web = urllib2.Request(results_web)
agent = random.choice(header)
request_web.add_header('User-Agent', agent)
opener_web = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
text = opener_web.open(request_web).read()
strreg = re.compile('(?<=href=")(.*?)(?=")')
names = strreg.findall(text)
counter += 100
for name in names:
if name not in uRLS:
if re.search(r'\(', name) or re.search("<", name) or re.search("\A/", name) or re.search("\A(http://)\d", name):
pass
elif re.search("google", name) or re.search("youtube", name) or re.search(".gov", name) or re.search("%", name):
pass
else:
uRLS.append(name)
tmpList = []; finalList = []
for entry in uRLS:
try:
t2host = entry.split("/",3)
domain = t2host[2]
if domain not in tmpList and "=" in entry:
finalList.append(entry)
tmpList.append(domain)
except:
pass
print "[+] URLS (sorted) :", len(finalList)
return finalList
我还做了很多修改,但还是没有任何变化!请告诉我我哪里错了……谢谢大家 :)
2 个回答
0
jro说得对,而且谷歌会定期改变他们搜索结果的格式,虽然不是每个月都改,但一年会改好几次,这样的话,你的正则表达式可能就会失效,你需要去修改它。
我以前也遇到过类似的问题,我选择了一个简单的解决方案,这些人提供了一个谷歌抓取工具,可以从搜索结果中提取所有链接,效果很好。你只需要输入关键词,它们就会抓取和解析谷歌的结果,并返回链接、标题、描述等等。这是另一种解决方案,但也可能对你有帮助。
1
我看到这里有两个问题。首先,你使用的自定义谷歌搜索似乎只返回来自google.com的结果。再加上一个正则表达式,它会检查网址中是否包含“google”(re.search("google", name)
),如果找到了,就不会把它加入网址列表,这样一来,这个自定义搜索的结果列表就总是空的。
其次,更重要的是,你的逻辑不太对。按照你现在的固定格式,你的代码是这样的:
if name not in uRLS:
if re.search(r'\(', name) or re.search("<", name) or re.search("\A/", name) or re.search("\A(http://)\d", name):
pass
elif re.search("google", name) or re.search("youtube", name) or re.search(".gov", name) or re.search("%", name):
pass
else:
uRLS.append(name)
(注意,elif
和else
可能缩进多了一次,但问题依然存在。)
因为你在检查name
是否不在uRLS
中,所以name
永远不会被加入到这个列表里,因为添加的逻辑在你的else
路径中。
要解决这个问题,去掉else
,减少append
语句的缩进,并把pass
语句替换成continue
。