bs4模块基本使用:
下载命令:pip install bs4
例子:
# 开发时间: 2022/9/14 17:21
import requests
import time
# 1.导包
from bs4 import BeautifulSoup
url = "https://www.umei.cc/e/action/get_img_a.php"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
}
dat = {
"next": 1,
"table": "news",
"action": "getmorenews",
"limit": 10,
"small_length": 120,
"classid": 23
}
resp = requests.post(url, headers=headers, data=dat)
# 2.使用 resp.text是拿到的源码
main_page = BeautifulSoup(resp.text, "html.parser")
# 3.bs4拿到内容 具体语法请看文档
data1 = main_page.find("ul", class_="pic-list after").find_all("img")
for a in data1:
href = a.get("src")
# print(href)
# 下载图片
img_resp = requests.get(href)
img_name = href.split("/")[-1] # 图片name
with open("09_img/" + img_name, mode="wb") as f:
f.write(img_resp.content)
print("orve", img_name)
time.sleep(1)