【发布时间】:2017-04-19 06:00:39
【问题描述】:
import requests
from lxml import html
page = requests.get('http://www.cnn.com')
html_content = html.fromstring(page.content)
for i in html_content.iterchildren():
print i
news_stories = html_content.xpath('//h2[@data-analytics]/a/span/text()')
news_links = html_content.xpath('//h2[@data-analytics]/a/@href')
我正在尝试运行此代码以了解 python 中的网络抓取是如何工作的。
我想从 CNN 中删除头条新闻及其链接。
当我在 Python Shell 中运行它时,我得到的 news_stories 和 news_links 的输出是:
[]
我的问题是我哪里出了问题,有没有比这个更好的方法来实现我想要的?
【问题讨论】:
标签: python