【问题标题】:how to get urls from google query?如何从谷歌查询中获取网址?
【发布时间】:2012-04-05 07:07:50
【问题描述】:

大家好,我尝试从谷歌获取网址,但它返回 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

我也做了很多编辑,但仍然没有任何反应!请告诉我我的错误是什么..谢谢大家:)

【问题讨论】:

  • 请修正缩进。它似乎是完全随机的。

标签: python html url google-search


【解决方案1】:

我看到了两个问题。首先,您使用的自定义 Google 搜索(显然)似乎只返回来自 google.com 的结果。这与在 url (re.search("google", name)) 中查找“google”出现的正则表达式相结合,当找到时 not 将其添加到 url 列表将导致 url 列表始终对此自定义搜索保持空白。

此外,更重要的是,您的逻辑不正确。使用固定格式,您目前执行以下操作:

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)

(请注意,elifelse 可能会缩进一次,但问题仍然存在。)

因为您检查name 是否不在uRLS 中,所以name 永远不会被添加到该列表中,因为添加逻辑在您的else 路径中。

要修复它,请删除else,减少append 语句的缩进,并将pass 语句替换为continue

【讨论】:

  • 1.由于您的自定义搜索只返回带有“google”的网址,re.search("google", name) 检查总是返回True:删除此函数调用。 --- 2.把我贴的sn-p中else的行去掉,把uRLS.append(name)前面的四个空格去掉。
【解决方案2】:

jro 是对的,而且 Google 会定期更改其结果的格式,不是每月一次,而是每年不止一次,那么您的正则表达式可能会失败,您需要对其进行修改。

我过去面临与您类似的问题,我选择了一个简单的解决方案,这些人提供了一个效果很好的 google scraper to extract all URLs from search results,您提供关键字,他们会抓取并解析 Google 结果并返回链接、锚点、描述等。这是解决方案的另一种方法,但它也可以为您提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多