【发布时间】:2015-10-06 10:50:10
【问题描述】:
我正在尝试使用我所拥有的有限知识制作一个 python 脚本来从网页中抓取特定信息。但我想我有限的知识是不够的。 我需要提取7-8条信息。标签如下 -
1
<a class="ui-magnifier-glass" href="here goes the link that i want to extract" data-spm-anchor-id="0.0.0.0" style="width: 258px; height: 258px; position: absolute; left: -1px; top: -1px; display: none;"></a>
2
<a href="link to extract" title="title to extract" rel="category tag" data-spm-anchor-id="0.0.0.0">or maybe this word instead of title</a>
如果我知道如何从这些 href 标签中提取信息。我将能够自己完成其余的工作。
如果有人可以帮助我编写代码以在 csv 文件中添加此信息,我们将不胜感激。
我已经开始使用此代码
url = raw_input('url : ')
page = requests.get(url)
tree = html.fromstring(page.text)
productname = tree.xpath('//h1[@class="product-name"]/text()')
price = tree.xpath('//span[@id="sku-discount-price"]/text()')
print '\n' + productname[0]
print '\n' + price[0]
【问题讨论】:
-
你想要使用
Beautifulsoup的解析方式,因为你已经在这里标记了它?我认为使用Beautifulsoup进行解析是迄今为止最简单的。
标签: python html beautifulsoup lxml python-requests