2017-01-04 37 views
0

我正在研究一个项目,通过google新闻头条来查找关键字。如何使用python获取谷歌新闻标题和搜索关键字?

我希望它: -put的头条新闻到一个文本文件 -remove逗号,顿号,引号,标点符号等 - 搜索关键字

这是我到目前为止的代码。我得到的头条新闻,我现在只需要它来解析每个标题的关键字。

from lxml import html 
import requests 

# Send request to get the web page 
response = requests.get('http://news.google.com') 

# Check if the request succeeded (response code 200) 
if (response.status_code == 200): 

# Parse the html from the webpage 
pagehtml = html.fromstring(response.text) 

# search for news headlines 
news = pagehtml.xpath('//h2[@class="esc-lead-article-title"] \ 
         /a/span[@class="titletext"]/text()') 

# Print each news item in a new line 
print("\n".join(news)) 
+0

这里的,例如,其中一个标题:'复杂化的“Brexit”计划,英国的高级特使欧盟Resigns' ...显示输出应该是什么样的解析后, – Andersson

回答

0

好吧我修好了。

from lxml import html 
import requests 

# Send request to get the web page 
response = requests.get('https://news.google.com/news/section?cf=all&pz=1&topic=b&siidp=b458d5455b7379bd8193a061024cd11baa97&ict=ln') 

# Check if the request succeeded (response code 200) 
if (response.status_code == 200): 

# Parse the html from the webpage 
pagehtml = html.fromstring(response.text) 

# search for news headlines 
news = pagehtml.xpath('//h2[@class="esc-lead-article-title"] \ 
         /a/span[@class="titletext"]/text()') 

# Print each news item in a new line 
print("\n \n".join(news)) 

tf = open("headlines.txt", "w") 

tf.write("\n \n".join(news).lower()) 

tf.close() 
# puts as lower case in text file named headlines 

with open('headlines.txt', 'r') as inF: 
    for line in inF: 
     if 'inflation' in line: 
      print "\n" + " " + line 
# searches for 'inflation' (or whatever query) and prints in indented on a new line