简体   繁体   English

如何使用python获取Google新闻标题和搜索关键字?

[英]How to use python to get google news headlines and search keywords?

I am working on a project to look through google news headlines and find keywords. 我正在研究一个通过Google新闻头条查找关键字的项目。

I want it to: -put the headlines into a text file -remove commas, apostrophes, quotes, punctuation, etc -search keywords 我希望它:-将标题放入文本文件中-删除逗号,撇号,引号,标点符号等-搜索关键字

This is the code I have so far. 这是我到目前为止的代码。 I am getting the headlines, I now just need it to parse the keywords from each individual headline. 我得到了头条新闻,现在我只需要它来解析每个单独的头条新闻中的关键字。

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))

Alright I fixed it. 好吧,我修复了它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM