【发布时间】:2017-03-01 04:45:41
【问题描述】:
我正在编写一个更大的代码,它将显示 Google 报纸搜索结果的链接,然后分析这些链接的某些关键字、上下文和数据。我已经完成了这一部分的所有工作,现在当我尝试遍历结果页面时,我遇到了一个问题。如果没有我不知道如何使用的 API,我不确定如何做到这一点。我只需要能够遍历搜索结果的多个页面,然后我就可以对其进行分析。似乎有一个简单的解决方案来遍历结果页面,但我没有看到它。
对解决此问题的方法有什么建议吗?我对 Python 有点陌生,并且一直在自学所有这些抓取技术,所以我不确定我是否只是在这里遗漏了一些简单的东西。我知道这可能是谷歌限制自动搜索的问题,但即使拉入前 100 个左右的链接也是有益的。我在常规的 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)
【问题讨论】:
-
看看这个replit.com我最近写的就是这样做。
标签: python google-analytics web-scraping offset google-news