yagooglesearch
는 지능적이고 사실적이며 조정 가능한 Google 검색을 실행하기 위한 Python 라이브러리입니다. 이는 Google의 속도 제한(두려운 HTTP 429 응답)을 방지하기 위해 실제 인간 Google 검색 동작을 시뮬레이션하고, Google에 의해 HTTP 429가 차단된 경우 물러서서 계속 시도하는 논리입니다. 라이브러리는 Google API를 사용하지 않으며 googlesearch 라이브러리를 기반으로 합니다. 기능은 다음과 같습니다:
requests
라이브러리 활용이 코드는 있는 그대로 제공되며 사용 방법에 대한 책임은 전적으로 귀하에게 있습니다. Google 검색 결과를 스크랩하는 것은 서비스 약관을 위반할 수 있습니다. 또 다른 Python Google 검색 라이브러리에는 이에 대한 흥미로운 정보/토론이 있습니다.
Google이 선호하는 방법은 API를 사용하는 것입니다.
pip install yagooglesearch
git clone https://github.com/opsdisk/yagooglesearch
cd yagooglesearch
virtualenv -p python3 .venv # If using a virtual environment.
source .venv/bin/activate # If using a virtual environment.
pip install . # Reads from pyproject.toml
import yagooglesearch
query = "site:github.com"
client = yagooglesearch . SearchClient (
query ,
tbs = "li:1" ,
max_search_result_urls_to_return = 100 ,
http_429_cool_off_time_in_minutes = 45 ,
http_429_cool_off_factor = 1.5 ,
# proxy="socks5h://127.0.0.1:9050",
verbosity = 5 ,
verbose_output = True , # False (only URLs) or True (rank, title, description, and URL)
)
client . assign_random_user_agent ()
urls = client . search ()
len ( urls )
for url in urls :
print ( url )
GUI를 통해 Google을 검색하면 "약 13,000,000개의 결과"와 같은 메시지가 표시되지만 이는 yagooglesearch
그에 가까운 것을 찾을 수 있다는 의미는 아닙니다. 테스트 결과 최대 약 400개의 결과가 반환되는 것으로 나타났습니다. 400 < max_search_result_urls_to_return
으로 설정하면 경고 메시지가 로그에 인쇄됩니다. 토론은 #28을 참조하세요.
낮고 느린 것은 yagooglesearch
사용하여 Google 검색을 실행할 때의 전략입니다. HTTP 429 응답을 받기 시작하면 Google은 귀하를 봇으로 정당하게 감지하고 일정 기간 동안 귀하의 IP를 차단합니다. yagooglesearch
CAPTCHA를 우회할 수 없지만 브라우저에서 Google 검색을 수행하고 사용자임을 증명하면 수동으로 이를 수행할 수 있습니다.
차단 기준과 임계값은 알려져 있지 않지만 일반적으로 사용자 에이전트를 무작위로 지정하고, 페이징된 검색 결과 간에 충분한 시간(7~17초)을 기다리고, 여러 Google 검색 간에 충분한 시간(30~60초)을 기다리는 것으로 충분합니다. 하지만 마일리지는 확실히 다양합니다. Tor와 함께 이 라이브러리를 사용하면 빠르게 차단될 가능성이 높습니다.
yagooglesearch
Google로부터 HTTP 429 응답을 감지하면 http_429_cool_off_time_in_minutes
분 동안 절전 모드로 전환된 후 다시 시도합니다. HTTP 429가 감지될 때마다 대기 시간이 http_429_cool_off_factor
배로 늘어납니다.
목표는 yagooglesearch
HTTP 429 감지 및 복구에 대해 걱정하고 이를 사용하는 스크립트에 부담을 주지 않도록 하는 것입니다.
yagooglesearch
HTTP 429를 처리하는 것을 원하지 않고 직접 처리하려는 경우 yagooglesearch 개체를 인스턴스화할 때 yagooglesearch_manages_http_429s=False
전달하세요. HTTP 429가 감지되면 "HTTP_429_DETECTED" 문자열이 반환될 목록 개체에 추가되며, 다음 단계는 사용자에게 달려 있습니다. 목록 개체에는 HTTP 429가 감지되기 전에 발견된 모든 URL이 포함됩니다.
import yagooglesearch
query = "site:twitter.com"
client = yagooglesearch . SearchClient (
query ,
tbs = "li:1" ,
verbosity = 4 ,
num = 10 ,
max_search_result_urls_to_return = 1000 ,
minimum_delay_between_paged_results_in_seconds = 1 ,
yagooglesearch_manages_http_429s = False , # Add to manage HTTP 429s.
)
client . assign_random_user_agent ()
urls = client . search ()
if "HTTP_429_DETECTED" in urls :
print ( "HTTP 429 detected...it's up to you to modify your search." )
# Remove HTTP_429_DETECTED from list.
urls . remove ( "HTTP_429_DETECTED" )
print ( "URLs found before HTTP 429 detected..." )
for url in urls :
print ( url )
yagooglesearch
프록시 사용을 지원합니다. 제공된 프록시는 검색의 다양한 부분에 대해 다양한 프록시를 순환하는 대신 검색의 전체 수명 주기에 사용되어 보다 인간적으로 보이도록 만듭니다. 일반적인 검색 수명 주기는 다음과 같습니다.
google.com
에 대한 '탐색' 시뮬레이션 프록시를 사용하려면 yagooglesearch.SearchClient
개체를 초기화할 때 프록시 문자열을 제공하세요.
client = yagooglesearch . SearchClient (
"site:github.com" ,
proxy = "socks5h://127.0.0.1:9050" ,
)
지원되는 프록시 체계는 Python requests
라이브러리(https://docs.python-requests.org/en/master/user/advanced/#proxies)에서 지원되는 프록시 체계를 기반으로 합니다.
http
https
socks5
- "프록시 서버가 아닌 클라이언트에서 DNS 확인이 이루어지도록 합니다." 모든 DNS 조회는 프록시 대신 yagooglesearch
실행되는 곳에서 발생하므로 이를 원하지 않을 것 입니다.socks5h
- "프록시 서버에서 도메인을 확인하려면 양말5h를 구성표로 사용하세요." DNS 조회 및 Google 검색이 프록시 IP 주소에서 제공되므로 SOCKS를 사용하는 경우 이것이 가장 좋은 옵션입니다. HTTPS 프록시에 자체 서명된 인증서를 사용하는 경우 다음 중 하나의 경우 SSL/TLS 확인을 비활성화해야 할 수 있습니다.
yagooglesearch.SearchClient
객체 인스턴스화: import yagooglesearch
query = "site:github.com"
client = yagooglesearch . SearchClient (
query ,
proxy = "http://127.0.0.1:8080" ,
verify_ssl = False ,
verbosity = 5 ,
)
query = "site:github.com"
client = yagooglesearch . SearchClient (
query ,
proxy = "http://127.0.0.1:8080" ,
verbosity = 5 ,
)
client . verify_ssl = False
여러 프록시를 사용하려는 경우 해당 부담은 다른 프록시를 사용하여 새 yagooglesearch.SearchClient
객체를 인스턴스화하기 위해 yagooglesearch
라이브러리를 활용하는 스크립트에 있습니다. 다음은 프록시 목록을 반복하는 예입니다.
import yagooglesearch
proxies = [
"socks5h://127.0.0.1:9050" ,
"socks5h://127.0.0.1:9051" ,
"http://127.0.0.1:9052" , # HTTPS proxy with a self-signed SSL/TLS certificate.
]
search_queries = [
"python" ,
"site:github.com pagodo" ,
"peanut butter toast" ,
"are dragons real?" ,
"ssh tunneling" ,
]
proxy_rotation_index = 0
for search_query in search_queries :
# Rotate through the list of proxies using modulus to ensure the index is in the proxies list.
proxy_index = proxy_rotation_index % len ( proxies )
client = yagooglesearch . SearchClient (
search_query ,
proxy = proxies [ proxy_index ],
)
# Only disable SSL/TLS verification for the HTTPS proxy using a self-signed certificate.
if proxies [ proxy_index ]. startswith ( "http://" ):
client . verify_ssl = False
urls_list = client . search ()
print ( urls_list )
proxy_rotation_index += 1
GOOGLE_ABUSE_EXEMPTION
쿠키 값이 있는 경우 SearchClient
개체를 인스턴스화할 때 해당 값이 google_exemption
으로 전달될 수 있습니다.
&tbs=
매개변수는 축어적 또는 시간 기반 필터를 지정하는 데 사용됩니다.
&tbs=li:1
시간 필터 | &tbs= URL 매개변수 | 메모 |
---|---|---|
지난 시간 | qdr:h | |
지난 하루 | qdr:d | 지난 24시간 |
지난주 | qdr:w | |
지난달 | qdr:m | |
작년 | qdr:y | |
관습 | cdr:1,cd_min:2021년 1월 1일,cd_max:2021년 6월 1일 | yagooglesearch.get_tbs() 함수를 참조하세요. |
현재 .filter_search_result_urls()
함수는 "google"이라는 단어가 포함된 모든 URL을 제거합니다. 이는 반환된 검색 URL이 Google URL로 오염되는 것을 방지하기 위한 것입니다. site:google.com computer
와 같이 URL에 'google'이 포함될 수 있는 결과를 명시적으로 검색하려는 경우 이 점에 유의하세요.
BSD 3조 라이선스에 따라 배포됩니다. 자세한 내용은 라이센스를 참조하세요.
@opsdisk
프로젝트 링크: https://github.com/opsdisk/yagooglesearch