使用xpath抓取网页内容失败
我正在使用xpath来抓取亚马逊网页上的特定内容,但它不太管用。有没有人能给我一些建议?这是那个页面的链接: 一个链接
我想抓取这些内容:“有趣的信用卡大小的印刷品”
我用的代码在这里:
from lxml import html
import requests
url = 'http://www.amazon.co.uk/dp/B009CX5VN2'
page = requests.get(url)
tree = html.fromstring(page.text)
feature_bullets = tree.xpath('//*[@id="feature-bullets"]/ul/li[1]/span/text()')
但是feature_bullets总是为空。真的需要一些帮助。
1 个回答
1
我下载的HTML内容和你期待的有些不一样。这里有一个对我有效的表达式:
tree.xpath('//div[@id="technicalProductFeaturesATF"]/ul/li[1]/text()')
完整的程序:
from lxml import html
import requests
from pprint import pprint
url = 'http://www.amazon.co.uk/dp/B009CX5VN2'
page = requests.get(url)
tree = html.fromstring(page.text)
feature_bullets = tree.xpath('//div[@id="technicalProductFeaturesATF"]/ul/li/text()')
pprint(feature_bullets)
结果:
$ python foo.py
['Fun, credit card-sized prints',
'LCD film counter and shooting mode display',
'Camera mounted mirror for self portraits',
'Powered by CR2 Batteries, Built-in, Automatic electronic flash',
'Fujifilm Instax Mini 25 + 30 Instax Mini Film']