iT邦幫忙

0

圖檔可以爬下來,但卻開不了

  • 分享至 

  • xImage

小弟是python新手 使用visual studio code以下是我的程式碥
'''
import os,time,requests,imghdr,random
from bs4 import BeautifulSoup as bs
url='https://www.google.com/search?q=%E7%BE%8E%E5%A5%B3&rlz=1C2CAFB_enTW617TW617'
headers={'user-agent': 'Mozilla/5.0'}
photolimit=5
response=requests.get(url,headers)
sp=bs(response.content,'html.parser')
items=sp.find_all('img')
folder_name='./photo/'
if (os.path.exists(folder_name)==False):
os.mkdir(folder_name)
for index,item in enumerate(items,1):
if( index < photolimit):
html=requests.get(url,item.get('src'))
img_name=folder_name+str(index)+'.jpeg'
print(html,img_name)
with open(img_name,'wb')as file:
file.write(html.content)
file.flush()
file.close()
print('第{}張已下載......'.format(index))
time.sleep(random.uniform(1,3))
print('完成~')
'''

實在不知該如何下tag

player iT邦大師 1 級 ‧ 2020-06-08 21:09:09 檢舉
如果存檔的檔名先從item.get('src')去抓呢?
你可能抓到非jpeg的圖檔卻存成 .jpeg 的副檔名?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答