user7910719 user7910719 - 1 year ago 69
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 http://www.cellartracker.com/wine.asp?iWine=91411 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:

print(content_divs[0].text_content().strip())
IndexError: list index out of range


Here's my code:

import requests, lxml.html
page = requests.get('http://www.cellartracker.com/wine.asp?iWine=91411')
html = lxml.html.fromstring(page.content)
content_divs = html.xpath('//*[@id="wine_copy_inner"]/p/a[4]')
print(content_divs[0].text_content().strip())


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