【发布时间】:2018-07-30 02:09:25
【问题描述】:
我目前正在学习如何使用 XPath 从 HTML 文档中提取信息。我正在使用 python,并且在获取网页标题之类的值时没有问题,但是当我尝试获取表格中特定单元格的文本时,我只是返回了一个空值。
这是我的代码,我使用 chrome 复制了要从中获取值的表格单元格的 XPath。
from lxml import html
import requests
page = requests.get('https://en.wikipedia.org/wiki/List_of_Olympic_Games_host_cities')
tree = html.fromstring(page.content)
#This will get the cell text:
location = tree.xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr[1]/td[3]/text()')
print('Location: ', location)
【问题讨论】:
标签: python html xpath web web-crawler