正则之利用元素属性进行匹配

当爬虫或者数据清洗时,会遇到知道id、class以及name属性

来匹配信息,获取数据的时候。

以下即可,自己自行替换属性以及添加url最后改成符合自己

需求的匹配模板和匹配对象即可。需要注意的一点是,记得查看匹配对象的类型。

html = requests.get(url).text
html = BeautifulSoup(html,"html.parser")
# print(html)

imgUrls = html.findAll('div', class_="slide-bigpic")
# print(type(imgUrls))查看对象的类型,只有字符串可以
imgList =[]
for imgUrl in imgUrls:
imgUrl = str(imgUrl)
res = re.compile(r'data-ks-lazyload="(.*?)"',re.S|re.M)
img = re.findall(res,imgUrl)
imgList.append(img[0])print(imgList)

原文地址:https://www.cnblogs.com/yp19970/p/12356651.html