[原創]python爬蟲之BeautifulSoup,爬取網頁上所有圖片標題並存儲到本地文件
阿新 • • 發佈:2019-05-15
%20 分享圖片 本地 col cbc quest 執行 python div
from bs4 import BeautifulSoup import requests import re import os r = requests.get("https://re.jd.com/search?keyword=%E6%B0%B4%E6%9E%9C%20%E7%BD%91&keywordid=44195495794&re_dcp=202m0QjIIg==&traffic_source=1004&test=1&enc=utf8&cu=true&utm_source=baidu-search&utm_medium=cpc&utm_campaign=t_262767352_baidusearch&utm_term=44195495794_0_32d58cbc7f0f40e08d64a09fbc8c95c4") result = r.content # print(result) soup = BeautifulSoup(result,"html.parser") # print(soup.script.text) souptext = soup.find(type=‘text/javascript‘).text # print(souptext) pattern3 =re.compile(r‘\"ad_title_text\":\"(.*?\"),\"image_url\":\"(.*?\.(jpg|png))\"‘) patternresult3 = pattern3.findall(souptext)print(patternresult3) j = 0 for i in patternresult3: j = j+1 title = i[0].replace(‘ ‘,‘‘).replace(‘\"‘,‘1‘).replace(‘/‘,‘1‘) with open(os.getcwd()+‘\\jpg\\‘+title+str(j)+"."+i[2],"wb") as f: #在執行代碼前,需要先創建一個jpg的目錄,os.getcwd()用來獲取當前目錄 f.write(requests.get("https://img1.360buyimg.com/n6/"+i[1]).content)
運行結果如下:
[原創]python爬蟲之BeautifulSoup,爬取網頁上所有圖片標題並存儲到本地文件