【问题标题】:How can I search through Stack Overflow questions from a script?如何从脚本中搜索 Stack Overflow 问题?
【发布时间】:2010-09-16 20:10:58
【问题描述】:

给定一串关键字,例如“Python 最佳实践”,我想从 Python 脚本中获取包含该关键字的前 10 个 Stack Overflow 问题,按相关性 (?) 排序。我的目标是最终得到一个元组列表(标题、URL)。

我怎样才能做到这一点?您会考虑改为查询 Google 吗? (你会如何从 Python 中做到这一点?)

【问题讨论】:

    标签: python search scripting stackexchange-api


    【解决方案1】:

    您可以从有效的 HTTP 请求中截取返回的 HTML。但这会导致恶业,并失去享受良好睡眠的能力。

    【讨论】:

      【解决方案2】:

      由于 Stackoverflow 已经有了这个功能,您只需要获取搜索结果页面的内容并抓取您需要的信息即可。以下是按相关性搜索的 URL:

      https://stackoverflow.com/search?q=python+best+practices&sort=relevance

      如果您查看源代码,您会看到每个问题所需的信息都在一行中,如下所示:

      <h3><a href="/questions/5119/what-are-the-best-rss-feeds-for-programmersdevelopers#5150" class="answer-title">What are the best RSS feeds for programmers/developers?</a></h3>
      

      因此,您应该能够通过正则表达式搜索该形式的字符串来获得前十个。

      【讨论】:

        【解决方案3】:

        建议将 REST API 添加到 SO。 http://stackoverflow.uservoice.com/

        【讨论】:

          【解决方案4】:

          我只会使用 Pycurl 将搜索词连接到查询 uri。

          【讨论】:

            【解决方案5】:
            >>> from urllib import urlencode
            >>> params = urlencode({'q': 'python best practices', 'sort': 'relevance'})
            >>> params
            'q=python+best+practices&sort=relevance'
            >>> from urllib2 import urlopen
            >>> html = urlopen("http://stackoverflow.com/search?%s" % params).read()
            >>> import re
            >>> links = re.findall(r'<h3><a href="([^"]*)" class="answer-title">([^<]*)</a></h3>', html)
            >>> links
            [('/questions/5119/what-are-the-best-rss-feeds-for-programmersdevelopers#5150', 'What are the best RSS feeds for programmers/developers?'), ('/questions/3088/best-ways-to-teach-a-beginner-to-program#13185', 'Best ways to teach a beginner to program?'), ('/questions/13678/textual-versus-graphical-programming-languages#13886', 'Textual versus Graphical Programming Languages'), ('/questions/58968/what-defines-pythonian-or-pythonic#59877', 'What defines &#8220;pythonian&#8221; or &#8220;pythonic&#8221;?'), ('/questions/592/cxoracle-how-do-i-access-oracle-from-python#62392', 'cx_Oracle - How do I access Oracle from Python? '), ('/questions/7170/recommendation-for-straight-forward-python-frameworks#83608', 'Recommendation for straight-forward python frameworks'), ('/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python#100903', 'Why is if not someobj: better than if someobj == None: in Python?'), ('/questions/132734/presentations-on-switching-from-perl-to-python#134006', 'Presentations on switching from Perl to Python'), ('/questions/136977/after-c-python-or-java#138442', 'After C++ - Python or Java?')]
            >>> from urlparse import urljoin
            >>> links = [(urljoin('http://stackoverflow.com/', url), title) for url,title in links]
            >>> links
            [('http://stackoverflow.com/questions/5119/what-are-the-best-rss-feeds-for-programmersdevelopers#5150', 'What are the best RSS feeds for programmers/developers?'), ('http://stackoverflow.com/questions/3088/best-ways-to-teach-a-beginner-to-program#13185', 'Best ways to teach a beginner to program?'), ('http://stackoverflow.com/questions/13678/textual-versus-graphical-programming-languages#13886', 'Textual versus Graphical Programming Languages'), ('http://stackoverflow.com/questions/58968/what-defines-pythonian-or-pythonic#59877', 'What defines &#8220;pythonian&#8221; or &#8220;pythonic&#8221;?'), ('http://stackoverflow.com/questions/592/cxoracle-how-do-i-access-oracle-from-python#62392', 'cx_Oracle - How do I access Oracle from Python? '), ('http://stackoverflow.com/questions/7170/recommendation-for-straight-forward-python-frameworks#83608', 'Recommendation for straight-forward python frameworks'), ('http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python#100903', 'Why is if not someobj: better than if someobj == None: in Python?'), ('http://stackoverflow.com/questions/132734/presentations-on-switching-from-perl-to-python#134006', 'Presentations on switching from Perl to Python'), ('http://stackoverflow.com/questions/136977/after-c-python-or-java#138442', 'After C++ - Python or Java?')]
            

            将其转换为函数应该很简单。

            编辑:哎呀,我会做的......

            def get_stackoverflow(query):
                import urllib, urllib2, re, urlparse
                params = urllib.urlencode({'q': query, 'sort': 'relevance'})
                html = urllib2.urlopen("http://stackoverflow.com/search?%s" % params).read()
                links = re.findall(r'<h3><a href="([^"]*)" class="answer-title">([^<]*)</a></h3>', html)
                links = [(urlparse.urljoin('http://stackoverflow.com/', url), title) for url,title in links]
            
                return links
            

            【讨论】:

              猜你喜欢
              • 2011-03-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-01-21
              • 1970-01-01
              • 2011-03-20
              • 1970-01-01
              相关资源
              最近更新 更多