from typing import Dict
import os
import requests
import re
from bs4 import BeautifulSoup
# import platform
# print(platform.machine())
myheaders: Dict[str, str] = {
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36‘}
url = ‘http://www.cntour.cn/‘
strtext = requests.get(url, headers=myheaders)
soup = BeautifulSoup(strtext.text, ‘lxml‘)
data = soup.select(‘#main>div>div.mtop.firstMod.clearfix>div.centerBox>ul.newsList>li>a‘)
# print(soup)
fileDir = ‘tupian‘
dataList = soup.find_all(‘img‘)
if not os.path.exists(fileDir):
print(‘create a filepath‘)
os.mkdir(os.path.join(os.getcwd(), fileDir))
fileDir = os.path.join(os.getcwd(), fileDir)
j = 1
for b in dataList:
picName = str(j) + ‘.jpg‘
picUrl = url + b.get(‘src‘)
# print(b.get(‘src‘),b.get(‘alt‘))
# print(os.path.join(os.getcwd(), filedir))
fPath = os.path.join(fileDir, picName)
print(fPath)
result = requests.get(url=picUrl)
with open(fPath, ‘wb+‘)as f: # 循环写入图片
f.write(result.content)
j = j + 1
print(‘success‘)
原文:https://www.cnblogs.com/my85016629/p/13686326.html