python爬取網(wǎng)頁數(shù)據(jù)到保存到csv
任務(wù)需求:
爬取一個網(wǎng)址,將網(wǎng)址的數(shù)據(jù)保存到csv中。
爬取網(wǎng)址:
https://www.iqiyi.com/ranks1/1/0?vfrm=pcw_home&vfrmblk=&vfrmrst=712211_dianyingbang_rebo_title
網(wǎng)址頁面:
代碼實現(xiàn)結(jié)果:
代碼實現(xiàn):
導(dǎo)入包:
import requests import parsel import csv
設(shè)置csv文件格式:
設(shè)計未來數(shù)據(jù)的存儲形式。
#打開文件 f = open('whxixi.csv', mode='a',encoding='utf-8',newline='') #文件列名 csv_writer= csv.DictWriter(f,fieldnames=['電影名字', '彈幕總數(shù)', '新增評論', '電影鏈接', '電影日期', '電影類型', '電影演員', '電影介紹']) #輸入文件列名 csv_writer.writeheader()
獲取數(shù)據(jù):
獲取網(wǎng)頁的html,得到原始的數(shù)據(jù)( 得到的數(shù)據(jù)保存在response中)。
#選擇愛奇藝熱播榜的網(wǎng)址 url='https://www.iqiyi.com/ranks1/1/0?vfrm=pcw_home&vfrmblk=&vfrmrst=712211_dianyingbang_rebo_title' headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'} #獲取網(wǎng)址內(nèi)容,賦值 到response response = requests.get(url=url, headers=headers)
加工數(shù)據(jù):
對得到的網(wǎng)頁原始數(shù)據(jù)進行加工處理,即提取出有用的數(shù)據(jù)。備注,根據(jù)爬取的網(wǎng)頁進行調(diào)整css()里面的內(nèi)容,不同網(wǎng)站頁面的結(jié)構(gòu)不同,根據(jù)需要進行調(diào)整。(F12開發(fā)者模式)
#把response.text轉(zhuǎn)換為selector對象 可以使用re, css,x-path選擇器 webtext = parsel.Selector(response.text) #第一步篩選數(shù)據(jù),形成列表,可以使下次查找形成循環(huán) list=webtext.css('.rvi__list a') #再上一步的基礎(chǔ)上,使用循環(huán),進行提取數(shù)據(jù) for li in list: title= li.css(' .rvi__con .rvi__tit1::text').get() bulletcomments =li.css('.rvi__con .rvi__tag__box span:nth-child(1)::text').get() #彈幕總數(shù) newcomments =li.css(' .rvi__con .rvi__tag__box span:nth-child(2)::text').get() #新增評論數(shù) href = li.css(' ::attr(href)').get().replace('//','http://') movie_info=li.css(' .rvi__con .rvi__type1 span::text').get().split('/') year = movie_info[0].strip() type = movie_info[1].strip() actor = movie_info[2].strip() filmIntroduction=li.css(' .rvi__con p::text').get().strip() dic={ '電影名字':title, '彈幕總數(shù)':bulletcomments, '新增評論':newcomments, '電影鏈接':href, '電影日期':year, '電影類型':type, '電影演員':actor, '電影介紹':filmIntroduction } csv_writer.writerow(dic) #將數(shù)據(jù)輸入到csv文件中
完整代碼:
import requests import parsel import csv f = open('whxixi.csv', mode='a',encoding='utf-8',newline='') csv_writer= csv.DictWriter(f,fieldnames=['電影名字', '彈幕總數(shù)', '新增評論', '電影鏈接', '電影日期', '電影類型', '電影演員', '電影介紹']) csv_writer.writeheader() #選擇愛奇藝熱播榜的網(wǎng)址 url='https://www.iqiyi.com/ranks1/1/0?vfrm=pcw_home&vfrmblk=&vfrmrst=712211_dianyingbang_rebo_title' headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'} #獲取網(wǎng)址內(nèi)容,賦值 到response response = requests.get(url=url, headers=headers) #把response.text轉(zhuǎn)換為selector對象 可以使用re, css,x-path選擇器 webtext = parsel.Selector(response.text) #第一步篩選數(shù)據(jù),形成列表,可以使下次查找形成循環(huán) list=webtext.css('.rvi__list a') #再上一步的基礎(chǔ)上,使用循環(huán),進行提取數(shù)據(jù) for li in list: title= li.css(' .rvi__con .rvi__tit1::text').get() bulletcomments =li.css('.rvi__con .rvi__tag__box span:nth-child(1)::text').get() #彈幕總數(shù) newcomments =li.css(' .rvi__con .rvi__tag__box span:nth-child(2)::text').get() #新增評論數(shù) href = li.css(' ::attr(href)').get().replace('//','http://') movie_info=li.css(' .rvi__con .rvi__type1 span::text').get().split('/') year = movie_info[0].strip() type = movie_info[1].strip() actor = movie_info[2].strip() filmIntroduction=li.css(' .rvi__con p::text').get().strip() dic={ '電影名字':title, '彈幕總數(shù)':bulletcomments, '新增評論':newcomments, '電影鏈接':href, '電影日期':year, '電影類型':type, '電影演員':actor, '電影介紹':filmIntroduction } csv_writer.writerow(dic) #將數(shù)據(jù)輸入到csv文件中
總結(jié)
到此這篇關(guān)于python爬取網(wǎng)頁數(shù)據(jù)到保存到csv的文章就介紹到這了,更多相關(guān)python爬取網(wǎng)頁數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python爬蟲網(wǎng)頁元素定位術(shù)
- python爬蟲lxml庫解析xpath網(wǎng)頁過程示例
- 使用pyscript在網(wǎng)頁中撰寫Python程式的方法
- 一文教會你用Python獲取網(wǎng)頁指定內(nèi)容
- Python用requests模塊實現(xiàn)動態(tài)網(wǎng)頁爬蟲
- Python實現(xiàn)網(wǎng)頁文件轉(zhuǎn)PDF文件和PNG圖片的示例代碼
- requests.gPython?用requests.get獲取網(wǎng)頁內(nèi)容為空?’?’問題
- 利用Python自制網(wǎng)頁并實現(xiàn)一鍵自動生成探索性數(shù)據(jù)分析報告
相關(guān)文章
Django項目中添加ldap登陸認(rèn)證功能的實現(xiàn)
這篇文章主要介紹了Django項目中添加ldap登陸認(rèn)證功能的實現(xiàn),詳細(xì)介紹了django-auth-ldap的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04使用SimpleITK讀取和保存NIfTI/DICOM文件實例
這篇文章主要介紹了使用SimpleITK讀取和保存NIfTI/DICOM文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Django 狀態(tài)保持搭配與存儲的實現(xiàn)
本文主要介紹了Django 狀態(tài)保持搭配與存儲的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06分析總結(jié)Python數(shù)據(jù)化運營KMeans聚類
本文主要以 Python 使用 Keans 進行聚類分析的簡單舉例應(yīng)用介紹聚類分析,它是探索性數(shù)據(jù)挖掘的主要任務(wù),也是統(tǒng)計數(shù)據(jù)分析的常用技術(shù),用于許多領(lǐng)域2021-08-08