【发布时间】:2017-01-04 20:05:39
【问题描述】:
我正在做一个项目来查看谷歌新闻标题并查找关键字。
我希望它: - 将标题放入文本文件 -删除逗号、撇号、引号、标点符号等 - 搜索关键字
这是我到目前为止的代码。我正在获取标题,我现在只需要它来解析每个标题中的关键字。
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))
【问题讨论】:
-
这里是例如标题之一:
Complicating 'Brexit' Plans, Britain's Top Envoy to EU Resigns... 显示解析后的输出结果
标签: python web-scraping python-requests lxml