"파이썬 모범 사례"와 같은 키워드 문자열이 주어지면 해당 키워드가 포함 된 처음 10 개의 스택 오버플로 질문을 관련성 (?)으로 분류하여 파이썬 스크립트에서 얻고 싶습니다. 내 목표는 튜플 (제목, URL)의 목록으로 끝나는 것입니다.스크립트에서 스택 오버플로 질문을 검색하려면 어떻게해야합니까?
어떻게하면됩니까? 대신 Google에 문의 하시겠습니까? (파이썬에서 어떻게 할 것입니까?)
"파이썬 모범 사례"와 같은 키워드 문자열이 주어지면 해당 키워드가 포함 된 처음 10 개의 스택 오버플로 질문을 관련성 (?)으로 분류하여 파이썬 스크립트에서 얻고 싶습니다. 내 목표는 튜플 (제목, URL)의 목록으로 끝나는 것입니다.스크립트에서 스택 오버플로 질문을 검색하려면 어떻게해야합니까?
어떻게하면됩니까? 대신 Google에 문의 하시겠습니까? (파이썬에서 어떻게 할 것입니까?)
>>> 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 “pythonian” or “pythonic”?'), ('/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 “pythonian” or “pythonic”?'), ('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
유효한 HTTP 요청에서 반환 된 HTML을 스크래핑 할 수 있습니다. 그러나 그것은 나쁜 카르마와 즐거운 밤의 잠을 즐길 수있는 능력의 상실을 초래할 것입니다.
Stackoverflow에는 이미이 기능이 있으므로 검색 결과 페이지의 내용을 가져 와서 필요한 정보를 긁어 내면됩니다. 여기에 관련하여 검색을위한 URL은 다음과 같습니다
https://stackoverflow.com/search?q=python+best+practices&sort=relevance
하면 소스보기, 당신은 당신이 각 질문에 대한 필요한 정보는이 같은 라인에있는 것을 볼 수 있습니다 경우
<h3><a href="https://stackoverflow.com/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>
그래서 그 형태의 문자열에 대한 정규식 검색을 수행하면 처음 10 개를 가져올 수 있습니다.
SO에 REST API를 추가 할 것을 제안하십시오. http://stackoverflow.uservoice.com/
나는 검색어 uri에 검색어를 연결하기 위해 Pycurl을 사용합니다.