user7910719 user7910719 - 1 year ago 59
Python Question

Scraping text with xpath/lxml

I'm trying to scrape the text "2005-2013" from the text that says "drink between: 2005 - 2013" from using xpath/lxml, and am only able to do it for some of the other pages on this site, and not this one. Not sure what I'm doing wrong/if the xpath I have been copying from the element is incorrect

it tell's me:

IndexError: list index out of range

Here's my code:

import requests, lxml.html
page = requests.get('')
html = lxml.html.fromstring(page.content)
content_divs = html.xpath('//*[@id="wine_copy_inner"]/p/a[4]')

thanks for your help!!!

Answer Source

If you want to get "2005 - 2013" you might use below code

content = html.xpath('//a[@title="Source: Community"]/text()')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download