如何使用python获取google新闻标题和搜索关键词?
我正在做一个项目,通过谷歌新闻标题查找关键词 我希望它: -将标题放入文本文件中 -删除逗号、撇号、引号、标点符号等 -搜索关键字 这是我目前掌握的代码。我得到了标题,我现在只需要它来解析每个标题的关键字如何使用python获取google新闻标题和搜索关键词?,python,web-scraping,python-requests,lxml,Python,Web Scraping,Python Requests,Lxml,我正在做一个项目,通过谷歌新闻标题查找关键词 我希望它: -将标题放入文本文件中 -删除逗号、撇号、引号、标点符号等 -搜索关键字 这是我目前掌握的代码。我得到了标题,我现在只需要它来解析每个标题的关键字 from lxml import html import requests # Send request to get the web page response = requests.get('http://news.google.com') # Check if the request
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
好吧,我修好了
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
比如,这里有一条新闻标题:
使“英国脱欧”计划复杂化,英国驻欧盟最高特使辞职
。。。比如,有一条新闻标题:使“英国脱欧”计划复杂化,英国驻欧盟最高特使辞职
。。。显示在您的分析之后应该有什么输出如果标题的类
名称未知该怎么办?如果标题的类
名称未知该怎么办?