python爬虫进阶(初始)
生活随笔
收集整理的這篇文章主要介紹了
python爬虫进阶(初始)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
該內容主要是爬蟲爬取圖片以及html,屬于庫的基本內容,以后再在此基礎上進階更智能更全面的python代碼
整體框架大致
目標:
下載圖片
創建文件夾并在文件夾里加入東西
批量下載圖片到文件夾里
篩選數據
批量篩選指定數據到文件夾里
將數據導入excle表格
將數據繪制成圖表
1、爬圖進階
1+、爬取html內容
import urllib.requestresponse =urllib.request.urlopen('http://www.fishc.com') html =response.read().decode("utf-8") print(html)1++、批量爬取圖片到文件夾里
import urllib.request import ospath='images' os.path.exists(path) os.makedirs(path) #創建文件夾for i in range(1,10):j=i*100# 網絡上圖片的地址img_src = 'http://placekitten.com/'+str(j)+'/'+str(j)# 將遠程數據下載到本地,第二個參數就是要保存到本地的文件名urllib.request.urlretrieve(img_src,'E:/編程/python/網絡爬圖1/images/'+str(i)+'.jpg')1+++、另一種批量爬取
import requests from PIL import Image from io import BytesIOimg_src = 'https://img-my.csdn.net/uploads/201212/25/1356422284_1112.jpg' response = requests.get(img_src) image = Image.open(BytesIO(response.content)) image.save('D:/9.jpg')2、篩選標簽
import urllib.request import re #成功爬取當前頁面所有圖片地址response =urllib.request.urlopen('http://pic.hao123.com/meinv') html=response.read() html=html.decode("utf-8")par =r'<img src="(.*?)" alt="" style="width: 180px;"/>' html=re.findall(par,html)for each in html:print(each)3、篩選正文
import urllib.requesthtml = urllib.request.urlopen("https://www.douban.com/").read().decode("utf-8")# 整個html打印出來太多,這里我們就保存在文件中,再查看 of = open("E:/編程/python/網絡爬圖1/db_index.html","w") of.write("dasdasdas") of.close()4、文檔寫入
f=open("E:/編程/python/網絡爬圖1/file.txt","w") constant="i love you" f.write(constant) f.close()#可以寫入任何硬盤當中5、文檔讀取
f=open("E:/編程/python/網絡爬圖1/file.txt","r") constant = f.read() print(constant) f.close()6、文檔綜合整理
import urllib.requestresponse =urllib.request.urlopen('http://www.fishc.com') html =response.read().decode("utf-8") print(html)f=open("E:/編程/python/網絡爬圖1/file.html","w") f.write(html) f.close()#將html文件保存到本地7、創建文件夾
import ospath='D' os.path.exists(path) os.makedirs(path)#os.mkdir(path)確認是否創建成功8、導入excle表格
import pandas as pdj=pd.read_excel("E:/wps/賬單/4月份賬單.xlsx")print(j)9、將數據繪制成圖
import matplotlib.pylab as pyl import numpy as npyx=[1,2,3,4,5] y=[8,6,4,3,1]pyl.plot(x,y) #繪制成線 pyl.plot(x,y,'o') #標出點 pyl.show() #展示該圖9+、繪制直方圖
import matplotlib.pylab as pyl import numpy as npydata=[8,6,9,413,49,45,41,6] pyl.hist(data) pyl.show()總結
以上是生活随笔為你收集整理的python爬虫进阶(初始)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十种户型房子不能买 不能买的户型房子都有
- 下一篇: 全球最大在建海上风电场首次发电,每年能为