生活随笔
收集整理的這篇文章主要介紹了
Python实训day04pm【网络爬虫(文本、图片)】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- Python實(shí)訓(xùn)-15天-博客匯總表
目錄
1、網(wǎng)絡(luò)爬蟲
1.1、爬取文本
1.2、爬取圖片
2、其他知識(shí)點(diǎn)
上午題目講解昨天的基礎(chǔ)題目講解爬取非文本(圖片)
1、網(wǎng)絡(luò)爬蟲
#bili 視頻,爬下來 【能】【很多代碼】【我不會(huì),懶得研究,很費(fèi)勁。】
# 非文本爬取
? ? #圖片、音頻、視頻、文檔等
? ? #實(shí)現(xiàn)批量自動(dòng)化下載
實(shí)現(xiàn)步驟:
獲取資源的路徑 url訪問地址,得到數(shù)據(jù)(字節(jié)序列)本地存儲(chǔ)(寫文件)
1.1、爬取文本
打印全部的20本書的書名。在打印書名時(shí),打印每本書的價(jià)格。將排名前100的書的(書名和價(jià)格)全部打印出來。提醒:第3問,可以需要循環(huán)爬取(爬取5次才能得到排名前100的書籍信息)。將這100本數(shù)的書名和價(jià)格,爬出來并且寫入到本地的txt文檔中。 爬取豆瓣前100的圖書和價(jià)格并且本地化寫入文件:
爬取htmlBeautifulSoup對(duì)html解析找到我們需要的數(shù)據(jù)對(duì)數(shù)據(jù)進(jìn)行處理
'''
1.打印全部的20本書的書名。√
2.在打印書名時(shí),打印每本書的價(jià)格。√
3.將排名前100的書的(書名和價(jià)格)全部打印出來。提醒:第3問,可以需要循環(huán)爬取(爬取5次才能得到排名前100的書籍信息)。√
4.將這100本數(shù)的書名和價(jià)格,爬出來并且寫入到本地的txt文檔中。√爬取豆瓣前100的圖書和價(jià)格并且本地化寫入文件
1.爬取html
2.BeautifulSoup對(duì)html解析
3.找到我們需要的數(shù)據(jù)
4.對(duì)數(shù)據(jù)進(jìn)行處理
'''
import requestsfrom bs4 import BeautifulSoup as BS# 偽裝成瀏覽器
hds = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}# 存放爬取到的書的信息
books = {};# 分5次,爬取100條數(shù)據(jù)
for i in range(5):url = 'https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4?start={}&type=T'.format(i * 20);resp = requests.get(url, headers=hds);# print(resp.status_code) # 200正確接收到了響應(yīng)結(jié)果ct = resp.content.decode('utf-8');# print(ct); # 爬取下來的html代碼html = BS(ct, 'lxml'); # 用BeautifulSoup解析html代碼# 選擇器定位目標(biāo)元素:[所有a標(biāo)簽]sa = html.select('.subject-item .info h2 a') # 爬取a標(biāo)簽的內(nèi)容# print(len(sa)) # 確認(rèn)一下【尤其是自己寫代碼時(shí)】打印選擇器個(gè)數(shù)20# 定位價(jià)格ps = html.select('.subject-item .info .pub');# print(len(ps)) # 確認(rèn)一下【尤其是自己寫代碼時(shí)】打印選擇器個(gè)數(shù)20for i in range(len(sa)):a = sa[i];p = ps[i];bookname = a['title']; # 通過[屬性名]來獲取屬性值price = p.get_text().replace(' ', '').replace('\n', '');price = price.split('/')[-1].replace('元', '');books[bookname] = price;# print(books)
# print(list(books.items()))# 將數(shù)據(jù)寫入文件
f = open(r'C:\Users\lwx\Desktop\books.txt', 'w');
for b in list(books.items()):f.write(b[0] + ':' + b[1] + '\n');
f.close()print("文本爬取完成~")# for a in sa:
# # bookname = a.get_text().replace(' ','').replace('\n','');
# bookname = a['title']; # 通過[屬性名]來獲取屬性值
# print(bookname);
#
# for p in ps:
# price = p.get_text().replace(' ', '').replace('\n', '');
# # 按斜杠/分割,拿取最后一個(gè)元素
# price = price.split('/')[-1].replace('元', '');
# print(price)
1.2、爬取圖片
'''
非文本爬取圖片、音頻、視頻、文檔等實(shí)現(xiàn)批量自動(dòng)化下載
1.獲取資源的路徑 url
2.訪問地址,得到數(shù)據(jù)(字節(jié)序列)
3.本地存儲(chǔ)(寫文件)
'''import requests
from bs4 import BeautifulSoup as BS # as BS取別名# 偽裝成瀏覽器
hds = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'}us = [];# 分5次,爬取100條數(shù)據(jù)
for i in range(5):url = 'https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4?start={}&type=T'.format(i * 20);resp = requests.get(url, headers=hds);ct = resp.content.decode('utf-8');html = BS(ct, 'lxml');# 選擇器定位目標(biāo)元素 [所有img標(biāo)簽]imgs = html.select('.subject-item .pic img')# print(len(sa)) # 確認(rèn)一下for img in imgs:img_url = img['src']; # 獲取屬性值us.append(img_url);# 逐個(gè)請(qǐng)求us中的地址,得到數(shù)據(jù)
# 存儲(chǔ)到本地(寫文件)
i = 1;
for u in us:# 爬地址resp = requests.get(u, headers=hds);ct = resp.content; # 此處就沒有decode了,因?yàn)榕廊〉氖菆D片,非文本,不能解碼# 本地寫入f = open(r'C:\Users\lwx\Desktop\books\{}{}'.format(i, '.jpg'), 'wb');f.write(ct);f.close();print("下載完畢:", i)i = i + 1;print("圖片爬取完畢~")
2、其他知識(shí)點(diǎn)
?
Python正則表達(dá)式:?Python 正則表達(dá)式 | 菜鳥教程
總結(jié)
以上是生活随笔為你收集整理的Python实训day04pm【网络爬虫(文本、图片)】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。