【发布时间】:2019-01-18 04:17:19
【问题描述】:
我不是程序员,但我需要实现一个简单的 HTML 解析器。
经过一个简单的研究,我能够作为一个给定的例子来实现:
from lxml import html
import requests
page = requests.get('https://URL.COM')
tree = html.fromstring(page.content)
#This will create a list of buyers:
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//span[@class="item-price"]/text()')
print 'Buyers: ', buyers
print 'Prices: ', prices
如何使用 tree.xpath 解析所有以“.com.br”结尾并以“://”开头的单词
【问题讨论】:
-
可以添加html dummy sn-p吗?
-
为什么需要自己实现?只需使用bs4 反正你需要外部库那么为什么不使用 bs4 而不是 lxml?
-
这不是 xpath 解析的工作方式 - 您首先使用文档结构进行解析,而不是内容!如果“以
.com.br结尾并以://开头的单词”实际上是链接(<a href="...">标签),您可以使用xpath提取所有链接,然后过滤您想要的。
标签: python parsing html-parsing