【发布时间】:2015-10-05 18:42:29
【问题描述】:
给定:
import urllib2
from lxml import etree
url = "http://www.ebay.com/sch/i.html?rt=nc&LH_Complete=1&_nkw=Under+Armour+Dauntless+Backpack&LH_Sold=1&_sacat=0&LH_BIN=1&_from=R40&_sop=3&LH_ItemCondition=1000"
response = urllib2.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
URL 是一个标准的 Ebay 搜索结果页面,应用了一些过滤:
我希望提取产品价格,例如40.00 美元、34.95 美元等。
有几种可能的 XPath(由 Firebug、XPath Checker Firefox 插件和手动检查源提供):
/html/body/div[5]/div[2]/div[3]/div/div[1]/div/div[3]/div/div[1]/div/w-root/div/div/ul/li[1]/ul[1]/li[1]/span
id('item3d00cf865e')/x:ul[1]/x:li[1]/x:span
//span[@class ='bold bidsold']
选择后者:
xpathselector="//span[@class ='bold bidsold']"
tree.xpath(xpathselector) 然后按预期返回Element 对象列表。当我得到他们的.text 属性时,我会期望得到价格。但我得到的是:
In [17]: tree.xpath(xpathselector)
Out[17]:
['\n\t\t\t\t\t',
u' 1\xc2\xa0103.78',
'\n\t\t\t\t\t',
u' 1\xc2\xa0048.28',
'\n\t\t\t\t\t',
' 964.43',
'\n\t\t\t\t\t',
' 922.43',
'\n\t\t\t\t\t',
' 922.43',
'\n\t\t\t\t\t',
' 275.67',
'\n\t\t\t\t\t',
每个值中包含的值看起来像价格,但是 (i) 价格远高于网页上显示的价格,(ii) 我想知道所有换行符和制表符都在那里做什么。 我在试图提取价格时有什么根本错误吗?
我通常将 WebDriver 用于此类事情,并利用 css 选择器、xpath 和类查找元素。但在这种情况下,我不希望与浏览器交互,这就是我第一次使用urllib2 和lxml 的原因。
等等
【问题讨论】:
标签: python python-2.7 xpath lxml