从一个页面打印html并不能显示python和urllib2的所有源页面

2024-06-21 20:04:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在亚马逊的特定页面上阅读。你知道吗

req = urllib2.Request('http://www.amazon.com/Upright-Citizens-Brigade-Comedy-Improvisation/dp/0989387801/ref=lp_1_1_6/175-0367440-7496156?ie=UTF8&qid=1376827779&sr=1-6%20buybox._V181901516_.png)%20center%20top%20no-repeat;')
req.add_header('User-agent', 'Mozilla/5.0\
            (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko)\
            Chrome/23.0.1271.97 Safari/537.11')
response=urllib2.urlopen(req)
html = response.read()
print html

我试图从一个新的项目“25.00美元”的价格,显示在网页的源代码,但这部分没有显示在html打印。我做错了什么?你知道吗


Tags: comhttpamazonresponserequesthtmlwww页面
1条回答
网友
1楼 · 发布于 2024-06-21 20:04:06

您应该使用html解析器,如lxmlBeautifulSoup。下面是一个使用lxml的示例:

parser = etree.HTMLParser()
root = etree.fromstring(html, parser=parser)

print root.xpath('//td[@class="a-text-right dp-new-col"]/a/span/text()')[0]

印刷品:

$25.00

请注意,所需的标记及其值是使用xpath表达式找到的:

XPath, the XML Path Language, is a query language for selecting nodes from an XML document.

另请参见:

希望有帮助。你知道吗

相关问题 更多 >