正则=xpath=BeautifulSoup
from bs4 import BeautifulSoup as bsf
import urllib.request
data = urllib.request.urlopen('xxxx.com').read().decode('utf-8','ignore')
bs = bsf(data) #格式化输出
print(bs.prettify())
bs.title #bs.标签名 <title>hello</title>
bs.title.name # 'title'
bs.title.string # hello
bs.a.attrs # 获取<a> 中所有属性
bs.a["class"] = bs.a.get("class") #获取的是 class="xxx"中的 xxx
bs.find_all('a')
bs.find_all(['a','u']) #获取所有a,u节点的内容
k1 = bs.ul.contents #返回list
k2 = bs.ul.children #返回的是生成器
children = [ i for i in k2]

本文深入探讨了网页数据抓取中的三种关键方法:正则表达式、xpath和BeautifulSoup的使用技巧。通过实例代码展示了如何利用urllib.request获取网页源码,并运用BeautifulSoup进行格式化输出及数据解析,包括获取特定标签、属性和内容。
2880

被折叠的 条评论
为什么被折叠?



