日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫进阶(初始)

發布時間:2023/12/3 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫进阶(初始) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該內容主要是爬蟲爬取圖片以及html,屬于庫的基本內容,以后再在此基礎上進階更智能更全面的python代碼
整體框架大致

目標:
下載圖片
創建文件夾并在文件夾里加入東西
批量下載圖片到文件夾里
篩選數據
批量篩選指定數據到文件夾里
將數據導入excle表格
將數據繪制成圖表

1、爬圖進階

import urllib.requestresponse =urllib.request.urlopen('http://sc3.hao123img.com/data/fd5166d33dba874e15d4f8fb43be485d') cat_img =response.read()with open('cat_20_300.jpg','wb') as a:a.write(cat_img)

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爬虫进阶(初始)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。