如何使用python获取Google新闻标题和搜索关键字? [英] How to use python to get google news headlines and search keywords?

查看:397
本文介绍了如何使用python获取Google新闻标题和搜索关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,以浏览Google新闻头条并找到关键字.

I am working on a project to look through google news headlines and find keywords.

我希望它: 将标题放入文本文件 -删除逗号,撇号,引号,标点符号等 -搜索关键字

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

推荐答案

好的,我修复了它.

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

这篇关于如何使用python获取Google新闻标题和搜索关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆