편집 : Google 검색은 AJAX 호출 등을 사용하여 정의를 파악한 것으로 보입니다. 아래의 해결책은 작동하지 않습니다. 당신이 urllib2가 사용에 문제가있는 경우
, 내가 사용하는 많은 쉽게 좋은
Python Requests 패키지를 제안한다.
Google 정의 및 다른 정의를 얻으려고 절대적으로 헌신하는 경우 Google 검색 "정의"프로토콜을 사용하여 페이지에 대한 HTTP 요청을하는 것이 좋습니다. 예를 들어
는 :
https://www.google.com.au/search?q=define:test
그런 다음 HTML 결과를 저장하고 당신이 필요로하는 정의를 구문 분석합니다. 파이썬 HTML 파서의 일부 예로는 HTMLParser 모듈과 BeautifulSoup 모듈이 있습니다. 그러나,이 구문 분석 작업은 매우 간단하므로 기본 정규식이 충분해야합니다.
이
<div style="display:inline" data-dobid="dfn"> # the order of the style and the data-dobid can change
<span>definition goes here</span>
</div>
HTML 페이지에서 "시험"의 정의를 잡기 위해 정규 표현식의 예 : 다음과 같이 모든 정의가 저장되는 (!) 참고로
import re
definitions = re.findall(r'data-dobid="dfn".*?>.*?\<span>(.*?)</span>.*?</div>', html, re.DOTALL)
>>> len(definitions)
18
>>> definitions[0]
'a\n procedure intended to establish the quality, performance, or \nreliability of something, especially before it is taken into widespread \nuse.'
# Looks like you might need to remove the newlines
>>> definitions[5]
'the result of a medical examination or analytical procedure.'
는, 또한 Google Dictionary API, 거기에 존재하는 요청에 대한 응답으로 JSON 형식으로 정의 결과를 제공 할 수 있습니다.
python3에서 라이브러리가 작동합니까? [pygoogle] (https://code.google.com/p/pygoogle/) 또는 [google] (https://pypi.python.org/pypi/google/1.05)? –