나는 Google 신문 검색 결과의 링크를 표시 한 다음 특정 키워드 및 컨텍스트 및 데이터에 대한 해당 링크를 분석하는 큰 코드를 작성 중입니다. 이 모든 것이 작동하도록 모든 것을 얻었습니다. 결과의 페이지를 반복 할 때 문제가 생깁니다. API를 사용하지 않고이 작업을 수행하는 방법을 잘 모르겠습니다. 사용 방법을 모르겠습니다. 검색 결과를 여러 페이지에 걸쳐 반복 할 수 있어야 분석 결과를 적용 할 수 있습니다. 결과 페이지를 반복하는 간단한 해결책이있는 것처럼 보이지만 나는 그것을 보지 않을 것입니다.페이지를 통해 반복하는 파이썬 Google 검색
이 문제에 접근하는 방법에 대한 제안 사항이 있습니까? 나는 다소 파이썬에 익숙하지 않고 이러한 모든 근근이 살아있는 기술을 가르쳐 왔기 때문에 여기에 간단한 것을 놓치고 있는지 확실하지 않습니다. 나는 이것이 자동 검색을 제한하는 Google의 문제 일 수 있지만 첫 번째 링크 만 가져 오면 도움이 될 것임을 알고 있습니다. Google의 일반 검색에서 예를 보았지만 Google 신문 검색에서는 볼 수 없었습니다.
다음은 코드 본문입니다. 제안 사항이있는 행이 있으면 도움이됩니다. 미리 감사드립니다!
def get_page_tree(url):
page = requests.get(url=url, verify=False)
return html.fromstring(page.text)
def find_other_news_sources(initial_url):
forwarding_identifier = '/url?q='
google_news_search_url = "https://www.google.com/search?hl=en&gl=us&tbm=nws&authuser=0&q=ohio+pay-to-play&oq=ohio+pay-to-play&gs_l=news-cc.3..43j43i53.2737.7014.0.7207.16.6.0.10.10.0.64.327.6.6.0...0.0...1ac.1.NAJRCoza0Ro"
google_news_search_tree = get_page_tree(url=google_news_search_url)
other_news_sources_links = [a_link.replace(forwarding_identifier, '').split('&')[0] for a_link in google_news_search_tree.xpath('//a//@href') if forwarding_identifier in a_link]
return other_news_sources_links
links = find_other_news_sources("https://www.google.com/search? hl=en&gl=us&tbm=nws&authuser=0&q=ohio+pay-to-play&oq=ohio+pay-to-play&gs_l=news-cc.3..43j43i53.2737.7014.0.7207.16.6.0.10.10.0.64.327.6.6.0...0.0...1ac.1.NAJRCoza0Ro")
with open('textanalysistest.csv', 'wt') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
for row in links:
print(row)