【问题标题】:duckduckgo API not returning resultsduckduckgo API 不返回结果
【发布时间】:2012-07-28 04:16:19
【问题描述】:

编辑我现在意识到 API 根本不够用,甚至无法正常工作。 我想重定向我的问题,我希望能够使用他们的“我感觉很笨”来自动搜索duckduckgo。这样我就可以搜索“stackoverflow”并获得主页(“https://stackoverflow.com/”)作为我的结果。

我正在使用 duckduckgo API。 Here

而且我在使用的时候发现:

r = duckduckgo.query("example")

结果不反映手动搜索,即:

for result in r.results:
    print result

结果:

>>> 
>>> 

什么都没有。

results 中查找索引会导致越界错误,因为它是空的。

我应该如何获得搜索结果?

似乎 API(根据其记录的示例)应该回答问题并以r.answer.text 的形式给出一种“我感觉很笨拙”

但是该网站的制作方式使我无法使用常规方法对其进行搜索和解析。

我想知道我应该如何使用此 API 或来自此站点的任何其他方法解析搜索结果。

谢谢。

【问题讨论】:

    标签: python api parsing search


    【解决方案1】:

    如果您访问DuckDuck Go API Page,您会发现一些关于使用 API 的注意事项。第一个注释清楚地表明:

    由于这是一个零点击信息 API,大多数深度查询(非主题名称) 将是空白的。

    这是这些字段的列表:

    Abstract: ""
    AbstractText: ""
    AbstractSource: ""
    AbstractURL: ""
    Image: ""
    Heading: ""
    Answer: ""
    Redirect: ""
    AnswerType: ""
    Definition: ""
    DefinitionSource: ""
    DefinitionURL: ""
    RelatedTopics: [ ]
    Results: [ ]
    Type: ""
    

    所以可能有点遗憾,但是他们的 API 只是截断了一堆结果,并没有给你;可能工作得更快,似乎除了使用DuckDuckGo.com之外什么都做不了。

    因此,显然,在这种情况下 API 不是可行的方法。

    就我而言,我只看到了一种方法:从 duckduckgo.com 检索原始 html 并使用例如解析它html5lib(值得一提的是,他们的 html 结构良好)。

    还值得一提的是,解析 html 页面并不是最可靠的数据报废方式,因为 html 结构可以更改,而 API 通常会保持稳定,直到公开宣布更改。

    下面是如何使用BeautifulSoup 实现这种解析的示例:

    from BeautifulSoup import BeautifulSoup
    import urllib
    import re
    
    site = urllib.urlopen('http://duckduckgo.com/?q=example')
    data = site.read()
    
    parsed = BeautifulSoup(data)
    topics = parsed.findAll('div', {'id': 'zero_click_topics'})[0]
    results = topics.findAll('div', {'class': re.compile('results_*')})
    
    print results[0].text
    

    此脚本打印:

    u'Eixample, an inner suburb of Barcelona with distinctive architecture'
    

    在主页面直接查询的问题是它使用JavaScript来产生所需的结果(不是相关的主题),所以你只能使用HTML版本来获取结果。 HTML版本有不同的链接:

    让我们看看我们能得到什么:

    site = urllib.urlopen('http://duckduckgo.com/html/?q=example')
    data = site.read()
    parsed = BeautifulSoup(data)
    
    first_link = parsed.findAll('div', {'class': re.compile('links_main*')})[0].a['href']
    

    first_link 变量中存储的结果是指向搜索引擎输出的第一个结果(不是相关搜索)的链接:

    http://www.iana.org/domains/example

    要获取所有链接,您可以遍历找到的标签(可以以类似方式接收除链接之外的其他数据)

    for i in parsed.findAll('div', {'class': re.compile('links_main*')}):
        print i.a['href']
    
    http://www.iana.org/domains/example
    https://twitter.com/example
    https://www.facebook.com/leadingbyexample
    http://www.trythisforexample.com/
    http://www.myspace.com/leadingbyexample?_escaped_fragment_=
    https://www.youtube.com/watch?v=CLXt3yh2g0s
    https://en.wikipedia.org/wiki/Example_(musician)
    http://www.merriam-webster.com/dictionary/example
    ...
    

    请注意,纯 HTML 版本仅包含 results,对于 相关搜索,您必须使用 JavaScript 版本。 (没有html 部分网址)。

    【讨论】:

    • 谢谢。这有助于我了解问题所在,您在哪里找到的? :P 我尝试为duckduckgo的常规html页面编写一个解析器,但我遇到了问题,因为它使用java或其他东西,结果没有以正确的html格式出现......
    • BeautifulSoup 对我来说效果很好。会更新答案
    • 嗯,错了,你得到的结果是从相关搜索中得到的。
    • 这只是页面是一致的HTML的一个例子,你可以这样做得到所有其他结果
    • 那么使用 html 页面,我可以获得多个结果吗?
    【解决方案2】:

    在得到我接受并给予赏金的问题的答案后,我找到了一个不同的解决方案,为了完整起见,我想在此处添加该解决方案。非常感谢所有帮助我找到这个解决方案的人。尽管这不是我要求的解决方案,但它可能会在将来对某人有所帮助。

    在本网站上经过漫长而艰苦的对话并附上一些支持邮件后发现:https://duck.co/topic/strange-problem-when-searching-intel-with-my-script

    这是解决方案代码(来自上面发布的线程中的答案):

    >>> import duckduckgo
    >>> print duckduckgo.query('! Example').redirect.url
    http://www.iana.org/domains/example
    

    【讨论】:

    • 链接好像失效了
    • 是的,看起来是这样。对不起 - 我在这里发布的主题的主要观点。其余的大部分只是关于问题的来回讨论。
    【解决方案3】:

    试试:

    for result in r.results:
        print result.text
    

    【讨论】:

    • 同样的结果,什么都没有。问题是 r.results 是一个空数组,API 根本没有返回任何结果。
    • r.related 返回相关的搜索/查询,这不是我想要得到的……尽管在某些情况下它可能有用。显然这是一种“胶带解决方案”
    • 如果你尝试:api.duckduckgo.com/?q=example&format=xml&pretty=1 你也会得到空结果。
    • true,但显然我的代码不是在搜索“示例”,大多数其他内容也不会返回任何结果。
    【解决方案4】:

    如果适合你的应用,你也可以试试相关搜索

    r = duckduckgo.query("example")
    for i in r.related_searches:
        if i.text:
            print i.text
    

    这会产生:

    Eixample, an inner suburb of Barcelona with distinctive architecture
    Example (musician), a British musician
    example.com, example.net, example.org, example.edu  and .example, domain names reserved for use in documentation as examples
    HMS Example (P165), an Archer-class patrol and training vessel of the British Royal Navy
    The Example, a 1634 play by James Shirley
    The Example (comics), a 2009 graphic novel by Tom Taylor and Colin Wilson
    

    【讨论】:

      【解决方案5】:

      对于 python 3 用户,@Rostyslav Dzinko 代码的转录:

      import re, urllib
      import pandas as pd
      from bs4 import BeautifulSoup
      
      query = "your query"
      site = urllib.request.urlopen("http://duckduckgo.com/html/?q="+query)
      data = site.read()
      soup = BeautifulSoup(data, "html.parser")
      
      my_list = soup.find("div", {"id": "links"}).find_all("div", {'class': re.compile('.*web-result*.')})[0:15]
      
      
      (result__snippet, result_url) = ([] for i in range(2))
      
      for i in my_list:         
            try:
                  result__snippet.append(i.find("a", {"class": "result__snippet"}).get_text().strip("\n").strip())
            except:
                  result__snippet.append(None)
            try:
                  result_url.append(i.find("a", {"class": "result__url"}).get_text().strip("\n").strip())
            except:
                  result_url.append(None)
      

      【讨论】:

        猜你喜欢
        • 2021-04-04
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 2022-06-22
        • 2012-10-31
        • 2018-11-03
        • 2015-04-21
        • 1970-01-01
        相关资源
        最近更新 更多