0
点赞
收藏
分享

微信扫一扫

BeautifulSoup爬取墙纸

Go_Viola 2022-04-04 阅读 67
python爬虫

BeautifulSoup爬取墙纸

import bs4
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import time
url = 'https://desk.zol.com.cn/pc/'
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36'
}
resp = requests.get(url, headers=headers)
resp.encoding = 'gbk' #观察网页编码,为gbk格式。
page_content = BeautifulSoup(resp.text,"html.parser")
li = page_content.find("ul",attrs={"class":"pic-list2"}).find_all("a")
for a in li:
    href = a.get("href")
    text = a.find("em").text
    if href.endswith(".exe"):
        continue
    href = urljoin(url,href)
    img_result = requests.get(href,headers=headers)
    img_text = img_result.text
    img_page_source = BeautifulSoup(img_text,"html.parser") #html.parser必须加,解析为html
    img_a = img_page_source.find("img",attrs={"id":"bigImg"})
    img_src=img_a.get("src")
    img_url = img_src.split("/")[-1] #取网页背后的编码作为图片名称
    img_resp = requests.get(img_src) #请求图片
    print(img_url)
    with open(img_url,"w") as f:
        f.write(img_resp.content) #图片为二进制
    time.sleep(1)




举报

相关推荐

0 条评论