python爬取網頁數(shù)據(jù)到保存到csv
任務需求:
爬取一個網址,將網址的數(shù)據(jù)保存到csv中。
爬取網址:
https://www.iqiyi.com/ranks1/1/0?vfrm=pcw_home&vfrmblk=&vfrmrst=712211_dianyingbang_rebo_title
網址頁面:
代碼實現(xiàn)結果:
代碼實現(xiàn):
導入包:
import requests import parsel import csv
設置csv文件格式:
設計未來數(shù)據(jù)的存儲形式。
#打開文件 f = open('whxixi.csv', mode='a',encoding='utf-8',newline='') #文件列名 csv_writer= csv.DictWriter(f,fieldnames=['電影名字', '彈幕總數(shù)', '新增評論', '電影鏈接', '電影日期', '電影類型', '電影演員', '電影介紹']) #輸入文件列名 csv_writer.writeheader()
獲取數(shù)據(jù):
獲取網頁的html,得到原始的數(shù)據(jù)( 得到的數(shù)據(jù)保存在response中)。
#選擇愛奇藝熱播榜的網址 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'} #獲取網址內容,賦值 到response response = requests.get(url=url, headers=headers)
加工數(shù)據(jù):
對得到的網頁原始數(shù)據(jù)進行加工處理,即提取出有用的數(shù)據(jù)。備注,根據(jù)爬取的網頁進行調整css()里面的內容,不同網站頁面的結構不同,根據(jù)需要進行調整。(F12開發(fā)者模式)
#把response.text轉換為selector對象 可以使用re, css,x-path選擇器 webtext = parsel.Selector(response.text) #第一步篩選數(shù)據(jù),形成列表,可以使下次查找形成循環(huán) list=webtext.css('.rvi__list a') #再上一步的基礎上,使用循環(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() #選擇愛奇藝熱播榜的網址 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'} #獲取網址內容,賦值 到response response = requests.get(url=url, headers=headers) #把response.text轉換為selector對象 可以使用re, css,x-path選擇器 webtext = parsel.Selector(response.text) #第一步篩選數(shù)據(jù),形成列表,可以使下次查找形成循環(huán) list=webtext.css('.rvi__list a') #再上一步的基礎上,使用循環(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文件中
總結
到此這篇關于python爬取網頁數(shù)據(jù)到保存到csv的文章就介紹到這了,更多相關python爬取網頁數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django項目中添加ldap登陸認證功能的實現(xiàn)
這篇文章主要介紹了Django項目中添加ldap登陸認證功能的實現(xiàn),詳細介紹了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),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06分析總結Python數(shù)據(jù)化運營KMeans聚類
本文主要以 Python 使用 Keans 進行聚類分析的簡單舉例應用介紹聚類分析,它是探索性數(shù)據(jù)挖掘的主要任務,也是統(tǒng)計數(shù)據(jù)分析的常用技術,用于許多領域2021-08-08