豆瓣電影TOP250爬蟲及視覺化分析筆記
"""
-*- coding: utf-8 -*-
@Time : 2021/11/7 下午 4:25
@Author : SunGuoqi
@Website :
@Github:
"""
import re
import time
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 資料存放在列表裡
datas = []
# 遍歷十頁資料
for k in range(10):
print(" 正在抓取第 {} 頁資料 ...".format(k + 1))
url = ' + str(k * 25)
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
# 查詢電影連結
lists = soup.find_all('div', {'class': 'hd'})
# 遍歷每條電影連結
for item in lists:
href = item.a['href']
# 休息一下,防止被封
time.sleep(0.5)
# 請求每條電影,獲得詳細資訊
response = requests.get(href, headers=headers)
# 把獲取好的電影資料打包成 BeautifulSoup 物件
movie_soup = 外匯跟單gendan5.comBeautifulSoup(response.text, 'lxml')
# 解析每條電影資料
# 片名
name = movie_soup.find('span', {'property': 'v:itemreviewed'}).text.split(' ')[0]
# 上映年份
year = movie_soup.find('span', {'class': 'year'}).text.replace('(', '').replace(')', '')
# 評分
score = movie_soup.find('strong', {'property': 'v:average'}).text
# 評價人數
votes = movie_soup.find('span', {'property': 'v:votes'}).text
infos = movie_soup.find('div', {'id': 'info'}).text.split('\n')[1:11]
# infos 返回的是一個列表,我們只需要索引提取就好了
# 導演
director = infos[0].split(': ')[1]
# 編劇
scriptwriter = infos[1].split(': ')[1]
# 主演
actor = infos[2].split(': ')[1]
# 型別
filmtype = infos[3].split(': ')[1]
# 國家 / 地區
area = infos[4].split(': ')[1]
# 資料清洗一下
if '.' in area:
area = infos[5].split(': ')[1].split(' / ')[0]
# 語言
language = infos[6].split(': ')[1].split(' / ')[0]
else:
area = infos[4].split(': ')[1].split(' / ')[0]
# 語言
language = infos[5].split(': ')[1].split(' / ')[0]
if ' 大陸 ' in area or ' 香港 ' in area or ' 臺灣 ' in area:
area = ' 中國 '
if ' 戛納 ' in area:
area = ' 法國 '
# 時長
times0 = movie_soup.find(attrs={'property': 'v:runtime'}).text
times = re.findall('\d+', times0)[0]
# 將資料寫入列表
datas.append({
' 片名 ': name,
' 上映年份 ': year,
' 評分 ': score,
' 評價人數 ': votes,
' 導演 ': director,
' 編劇 ': scriptwriter,
' 主演 ': actor,
' 型別 ': filmtype,
' 國家 / 地區 ': area,
' 語言 ': language,
' 時長 ( 分鐘 )': times
})
print(" 電影《 {0} 》已爬取完成 ...".format(name))
# 寫入到檔案
df = pd.DataFrame(datas)
df.to_csv("top250.csv", index=False, header=True, encoding='utf_8_sig')
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2841373/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料視覺化豆瓣電影 TOP250視覺化
- Python爬取分析豆瓣電影Top250Python
- 爬取豆瓣電影Top250和資料分析
- 豆瓣電影資料分析和視覺化視覺化
- Python爬蟲筆記(4):利用scrapy爬取豆瓣電影250Python爬蟲筆記
- scrapy入門:豆瓣電影top250爬取
- 使用python爬取豆瓣電影TOP250Python
- [python爬蟲] BeautifulSoup和Selenium對比爬取豆瓣Top250電影資訊Python爬蟲
- 手把手教你網路爬蟲(爬取豆瓣電影top250,附帶原始碼)爬蟲原始碼
- 正規表示式_爬取豆瓣電影排行Top250
- 爬蟲教程——用Scrapy爬取豆瓣TOP250爬蟲
- Python 爬蟲實戰(1):分析豆瓣中最新電影的影評Python爬蟲
- 爬蟲01:爬取豆瓣電影TOP 250基本資訊爬蟲
- 專案之爬蟲入門(豆瓣TOP250)爬蟲
- Python爬蟲教程-17-ajax爬取例項(豆瓣電影)Python爬蟲
- 用python寫一個豆瓣短評通用爬蟲(登入、爬取、視覺化)Python爬蟲視覺化
- 爬蟲練手-豆瓣top250(go版以及python版)爬蟲GoPython
- 豆瓣top250資料爬取
- python——豆瓣top250爬取Python
- Python:獲取豆瓣電影TOP250列表Python
- python爬蟲知識點三--解析豆瓣top250資料Python爬蟲
- scrapy爬取豆瓣電影資料
- 【Python】從0開始寫爬蟲——轉身扒豆瓣電影Python爬蟲
- 06、豆瓣爬蟲爬蟲
- 爬蟲豆瓣美女爬蟲
- 最新Python爬蟲和資料視覺化Python爬蟲視覺化
- 同花順股票分數視覺化 | R爬蟲&視覺化第3季視覺化爬蟲
- Python爬蟲入門 | 7 分類爬取豆瓣電影,解決動態載入問題Python爬蟲
- 擼個爬蟲,爬取電影種子爬蟲
- python更換代理爬取豆瓣電影資料Python
- 全棧 - 9 實戰 爬取豆瓣電影資料全棧
- nodejs 30行程式碼 爬豆瓣電影資料NodeJS行程
- 爬蟲筆記(一)爬蟲筆記
- Python 招聘資訊爬取及視覺化Python視覺化
- Python爬蟲之小說資訊爬取與資料視覺化分析Python爬蟲視覺化
- 一篇文章教會你利用Python網路爬蟲實現豆瓣電影採集Python爬蟲
- 視覺化爬蟲Portia安裝和部署踩過的坑視覺化爬蟲
- 爬取B站視訊播放量及資料視覺化視覺化