一.python語句存儲
1.with open()語句
1 with open(name,mode,encoding) as file: 2 file.write()
name:包含文件名稱的字符串;
mode:決定了打開文件的模式,只讀/寫入/追加等;
encoding:表示我們要寫入數據的編碼,一般為 utf-8 或者 gbk ;
file:表示我們在代碼中對文件的命名。
2.w:只寫模式,如果沒有文件則自動創建
1 f.write("{} {} {} {}\n".format(title,price,scrible,pic))
3.例子
1)
1 with open('a.txt','wb') as f: 2 for tag in soup.find_all('div',class_='service-item-pic'): 3 a_url = tag.find('a').get('href') 4 f.write(a_url) 5 f.write('\n')
2)
1 for tag in soup.find_all('div',class_='service-item-pic'): 2 with open('3.txt', 'a') as f: 3 a_url = tag.find('a').get('href') 4 f.write(a_url) 5 f.write('\n')
二.保存圖片
1.方法
首先用Beautiful Soup結合正則表達式的方式來提取所有鏈接:
1 links = soup.find_all('img', "origin_image zh-lightbox-thumb",src=re.compile(r'.jpg$'))
提取出所有鏈接后,使用request.urlretrieve來將所有鏈接保存到本地
2.例子
1 import time 2 from urllib import request 3 from bs4 import BeautifulSoup 4 import re 5 url = r'https://www.zhihu.com/question/355015346/answer/892031308' 6 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} 7 page = request.Request(url, headers=headers) 8 page_info = request.urlopen(page).read().decode('utf-8') 9 soup = BeautifulSoup(page_info, 'html.parser') 10 links = soup.find_all('img', "origin_image zh-lightbox-thumb",src=re.compile(r'.jpg$')) 11 local_path = r'C:\Users\Administrator\Desktop' 12 for link in links: 13 print(link.attrs['src']) 14 request.urlretrieve(link.attrs['src'], local_path+r'\%s.jpg' % time.time())