[英]Scraping text with xpath/lxml
我正在嘗試使用xpath / lxml從http://www.cellartracker.com/wine.asp?iWine=91411中使用“2005-2013之間的飲料”文本中刪除“2005-2013”文本,並且我只能為本網站上的其他一些頁面做這件事,而不是這個。 不確定我做錯了/如果我從元素復制的xpath是不正確的
它告訴我:
print(content_divs[0].text_content().strip())
IndexError: list index out of range
這是我的代碼:
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())
謝謝你的幫助!!!
xpath數組是否為零索引?
//*[@id="wine_copy_inner"]/p/a[4]
應該是...[3]
。
貌似數量a
當您登錄VS登出標簽的變化。 可能想使用另一種方法來定位此標記。
如果你想獲得"2005 - 2013"
你可以使用下面的代碼
content = html.xpath('//a[@title="Source: Community"]/text()')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.