各位你好,初级编程想问下,为啥我下载的图片无法打开??谢谢各位了
import re
import requests
import csv
from bs4 import BeautifulSoup
import time
#获取源地址url
#获取图片的url,下载图片
url = 'http://qianye88.com/'
urldata = requests.get(url).text
#获取图片的源地址
soup = BeautifulSoup(urldata, 'html.parser')
#print(soup)
dierdata = soup.find('div',class_="category layout auto mt none")
#print(dierdata)
da = re.compile(r'',re.S)
data1 = dierdata.find_all('a')[1:16]
#print(data1)
data2 = data1[0]
#print(data2)
#print(data2['href'])
#获取第二个网址
url2 = 'http://qianye88.com/' + data2['href']
#print(url2)
hard = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'}
data5 = requests.get(url2,headers=hard)
data6= data5.text
data7 = BeautifulSoup(data6,'html.parser')
#print(data7)
data8 = data7.find_all('img',class_='lazy')
for data9 in data8:
print(data9)
data10 = data9['data-original']
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/248968640186120.png "#left")
data12 = data10.strip('!')
data11 = data9['alt']
print(data12)
#获取图片
data9 = requests.get(data10,headers=hard)
img_name =data10.split('/')[-1]
with open(r'E:\\tupian\\'+data11+'jpg', mode='wb') as f:
f.write(data9.content)#图片内容写入到文件夹
print('over',data11)
time.sleep(2)
print('下载完毕')
最后结果是这样的