代码之家  ›  专栏  ›  技术社区  ›  user2372074

使用xpath删除web内容将不起作用

  •  1
  • user2372074  · 技术社区  · 10 年前

    我正在使用xpath抓取一个亚马逊网页,但它不起作用。有人能给我一些建议吗?以下是该页面的链接: a link

    我想刮这些:“有趣的,信用卡大小的印刷品” 我使用的代码如下:

    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 回复  |  直到 10 年前
        1
  •  1
  •   Robᵩ    10 年前

    我下载的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']