啥都别说了,上代码,这应该是最简洁的写法了

import requests
from bs4 import BeautifulSoup
from lxml import etree

url = "http://www.doutula.com/"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.text, 'lxml')

# 保存网页html内容
with open('content.html', 'w', encoding="utf-8") as f:
    html = soup.prettify()
    f.write(html)

# 通过find_all()选择
html = soup.find_all('img', class_="img-responsive lazy image_dta")
links = [i['data-original'] for i in html]

# 通过css选择
html = soup.select("img[data-original]")
links = [i['data-original'] for i in html]

# 通过xpath选择
root = etree.HTML(response.text)
links = root.xpath('//img/@data-original')

# 保存图片
for index, link in enumerate(links):
    img = requests.get(link)
    with open('img/'+str(index)+'.jpg', 'wb+') as f:
        f.write(img.content)
Last modification:January 2nd, 2020 at 12:33 am