【发布时间】:2019-05-03 15:52:36
【问题描述】:
我尝试从网站中提取信息,但不幸的是我只能获得有限的范围。我对正确的 Xpath 有问题,它接收的不仅仅是整个表的第一个元素。为了显示 Xpath,我使用 Chrome DevTools。如何使 Xpath 更通用以获得所需的结果?或者有谁知道我怎样才能更聪明地做到这一点?我的目标是稍后获取一个 json 文件。
import requests
import lxml.html
html = requests.get('http://volcano.oregonstate.edu/volcano_table')
doc = lxml.html.fromstring(html.content)
volcanoes = doc.xpath('//*[@id="content"]/div/div[2]/table/tbody/tr[1]/td[1]/a/text()')
country = doc.xpath('//*[@id="content"]/div/div[2]/table/tbody/tr[1]/td[2]/text()')
latitude = doc.xpath('//*[@id="content"]/div/div[2]/table/tbody/tr[1]/td[4]/text()')
longitude = doc.xpath('//*[@id="content"]/div/div[2]/table/tbody/tr[1]/td[5]/text()')
elevation = doc.xpath('//*[@id="content"]/div/div[2]/table/tbody/tr[1]/td[6]/text()')
output = []
for info in zip(volcanoes, country, latitude, longitude, elevation):
resp = {}
resp['volcanoes'] = info[0]
resp['country'] = info[1]
resp['latitude'] = info[2]
resp['longitude'] = info[3]
resp['elevation'] = info[4]
output.append(resp)
print(output)
这是代码目前能够接收的内容:
[{'volcanoes': 'Abu', 'country': '\n Japan ', 'latitude': '\n 34.50 ', 'longitude': '\n 131.60 ', 'elevation': '\n 641 '}]
【问题讨论】:
标签: python-3.x web-scraping lxml