其他分享
首页 > 其他分享> > 爬虫基础-bs4模块

爬虫基础-bs4模块

作者:互联网

bs4基本使用:

find()和find_all():

bs4案例:

#爬取卖菜网最新消息
from bs4 import BeautifulSoup
import requests
url = "http://www.maicainan.com/"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40"
}
res = requests.get(url, headers=headers)
page = BeautifulSoup(res.text, "html.parser")
div = page.find("div", class_="im_list")
li = div.find_all("li")
for a in li:
    data = a.find_all("a")
    title = data[0].text
    print(title)
res.close()

标签:bs4,class,xxx,BeautifulSoup,爬虫,html,模块,find
来源: https://blog.csdn.net/qq_44091819/article/details/121895321