网页解析

RE

搜索匹配规则的所有内容

# 提取指定内容
s = """
<a href="http://www.badu.com/s?wd=hahaha">hahaha</a>
<a href="http://www.tmall.com/">tmall</a>
<a href="http://www.tmall.com/">tmall</a>
"""

result = re.findall('<a href="(.*?)">(.*?)</a>', s, re.S)
print(result)

# 输出:
[('http://www.badu.com/s?wd=hahaha', 'hahaha'), ('http://www.tmall.com/', 'tmall'), ('http://www.tmall.com/', 'tmall')]


# 解析:
# re.findall 搜索所有匹配的结果,返回一个列表
# ()用于优先输出,返回结果为元组

搜索第一个匹配到的值,并赋予变量名

BS4

XPath

案例:17k chapter html 获取章节链接

XPath 规则

Python 实现

html源码 (https://www.17k.com/list/3425715.htmlarrow-up-right)

file-download
32KB

案例:17K page html 获取文章内容文本

XPath 规则

html源码(https://www.17k.com/chapter/3425715/46114063.htmlarrow-up-right

file-download
39KB

Last updated