Python爬蟲爬取愛奇藝電影片庫首頁的實例代碼
更新時間:2021年05月06日 09:26:06 作者:楊傳偉
這篇文章主要介紹了Python爬蟲爬取愛奇藝電影片庫首頁的實例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
上篇文章給大家介紹了Python爬取愛奇藝電影信息代碼實例 感興趣的朋友點擊查看下。
今天給大家介紹Python爬蟲爬取愛奇藝電影片庫首頁,下面是實例代碼,參考下:
import time import traceback import requests from lxml import etree import re from bs4 import BeautifulSoup from lxml.html.diff import end_tag import json import pymysql #連接數(shù)據(jù)庫 獲取游標 def get_conn(): """ :return: 連接,游標 """ # 創(chuàng)建連接 conn = pymysql.connect(host="82.157.112.34", user="root", password="root", db="MovieRankings", charset="utf8") # 創(chuàng)建游標 cursor = conn.cursor() # 執(zhí)行完畢返回的結果集默認以元組顯示 if ((conn != None) & (cursor != None)): print("數(shù)據(jù)庫連接成功!游標創(chuàng)建成功!") else: print("數(shù)據(jù)庫連接失??!") return conn, cursor #關閉數(shù)據(jù)庫連接和游標 def close_conn(conn, cursor): if cursor: cursor.close() if conn: conn.close() return 1 def get_iqy(): # 獲取數(shù)據(jù)庫總數(shù)據(jù)條數(shù) conn, cursor = get_conn() sql = "select count(*) from movieiqy" cursor.execute(sql) # 執(zhí)行sql語句 conn.commit() # 提交事務 all_num = cursor.fetchall()[0][0] #cursor 返回值的類型是一個元祖的嵌套形式 比如( ( ) ,) pagenum=int(all_num/48)+1 #這里是計算一個下面循環(huán)的起始值 每48個電影分一組 print(pagenum) print("movieiqy數(shù)據(jù)庫有", all_num, "條數(shù)據(jù)!") url = "https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=1&ret_num=48&session=ee4d98ebb4e8e44c8d4b14fa90615fb7" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" } # response=requests.get(url=url,headers=headers) # response.encoding="utf-8" # page_text=response.text # print(page_text) """ """ # temp_list = [] #暫時存放單部電影的數(shù)據(jù) dataRes = [] #每次循環(huán)把單部電影數(shù)據(jù)放到這個list for i in range(pagenum+1, pagenum+100): #循環(huán)100-1次 url = "https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=1&ret_num=48&session=ee4d98ebb4e8e44c8d4b14fa90615fb7" url_0 = "https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=" url_0 = url_0 + str(i) + "&ret_num=48&session=ad1d98bb953b7e5852ff097c088d66f2" print(url_0) #輸出拼接好的url response = requests.get(url=url_0, headers=headers) response.encoding = "utf-8" page_text = response.text #解析json對象 json_obj = json.loads(page_text) #這里的異常捕獲是因為 測試循環(huán)的次數(shù)有可能超過電影網(wǎng)站提供的電影數(shù) 為了防止后續(xù)爬到空的json對象報錯 try: json_list = json_obj['data']['list'] except KeyError: return dataRes #json為空 程序結束 for j in json_list: # 開始循環(huán)遍歷json串 # print(json_list) name = j['name'] #找到電影名 print(name) temp_list.append(name) #異常捕獲,防止出現(xiàn)電影沒有評分的現(xiàn)象 try: score = j['score'] #找到電影評分 print(score) temp_list.append(score) except KeyError: print( "KeyError") temp_list.append("iqy暫無評分") #替換字符串 link = j['playUrl'] #找到電影鏈接 temp_list.append(link) # 解析播放狀態(tài) state = [] pay_text = j['payMarkUrl'] #因為播放狀態(tài)只有在一個圖片鏈接里有 所以需要使用re解析出類似vip和only(獨播)的字樣 if (len(pay_text) == 0): #如果沒有這個圖片鏈接 說明電影是免費播放 state="免費" else: find_state = re.compile("(.*?).png") state = re.findall(find_state, pay_text) #正則匹配鏈接找到vip if(len(state)!=0): #只有當鏈接不為空再執(zhí)行 # print(state) # 再次解析 state = state[0][0:3] #字符串分片 # 這里只輸出了三個字符,如果是獨播,頁面顯示的是only,我們設置為”獨播“ if (state == "onl"): state = "獨播" else: state = "VIP" # print(state) # 添加播放狀態(tài) temp_list.append(state) dataRes.append(temp_list) # print(temp_list) temp_list = [] print('___________________________') return dataRes def insert_iqy(): cursor = None conn = None try: count=0 list = get_iqy() print(f"{time.asctime()}開始插入愛奇藝電影數(shù)據(jù)") conn, cursor = get_conn() sql = "insert into movieiqy (id,name,score,path,state) values(%s,%s,%s,%s,%s)" for item in list: print(item) count = count + 1 if (count % 48 == 0): print('___________________________') #異常捕獲,防止數(shù)據(jù)庫主鍵沖突 try: cursor.execute(sql, [0, item[0], item[1], item[2], item[3] ]) except pymysql.err.IntegrityError: print("重復!跳過!") conn.commit() # 提交事務 update delete insert操作 print(f"{time.asctime()}插入愛奇藝電影數(shù)據(jù)完畢") except: traceback.print_exc() finally: close_conn(conn, cursor) return; if __name__ == '__main__': # get_iqy() insert_iqy()
到此這篇關于Python爬蟲爬取愛奇藝電影片庫首頁的實例代碼的文章就介紹到這了,更多相關Python爬取愛奇藝電影內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- python編程開發(fā)之textwrap文本樣式處理技巧
- Python的文本常量與字符串模板之string庫
- Python中使用subprocess庫創(chuàng)建附加進程
- Python超簡單容易上手的畫圖工具庫推薦
- python爬蟲請求庫httpx和parsel解析庫的使用測評
- Python高級文件操作之shutil庫詳解
- Python超簡單容易上手的畫圖工具庫(適合新手)
- python學習之panda數(shù)據(jù)分析核心支持庫
- Python基礎之操作MySQL數(shù)據(jù)庫
- Python繪圖庫Matplotlib的基本用法
- Python Excel處理庫openpyxl詳解
- python使用openpyxl庫讀寫Excel表格的方法(增刪改查操作)
- Python time庫的時間時鐘處理
- Python基礎之常用庫常用方法整理
- python數(shù)據(jù)庫批量插入數(shù)據(jù)的實現(xiàn)(executemany的使用)
- Python爬蟲之必備chardet庫
- python中requests庫+xpath+lxml簡單使用
- Python格式化文本段落之textwrap庫
相關文章
win10下Python3.6安裝、配置以及pip安裝包教程
下面小編就為大家?guī)硪黄獁in10下Python3.6安裝、配置以及pip安裝包教程。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Python爬蟲實例之2021貓眼票房字體加密反爬策略(粗略版)
這篇文章主要介紹了Python爬蟲實例之2021貓眼票房字體加密反爬策略(粗略版),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02使用Python內(nèi)置的模塊與函數(shù)進行不同進制的數(shù)的轉(zhuǎn)換
這篇文章主要介紹了使用Python內(nèi)置的模塊與函數(shù)進行不同進制的數(shù)的轉(zhuǎn)換的方法,Python也使得讀取純二進制文件內(nèi)容非常方便,需要的朋友可以參考下2016-03-03Django?使用VScode?創(chuàng)建工程的詳細步驟
這篇文章主要介紹了Django?使用VScode?創(chuàng)建工程,創(chuàng)建Django 項目,可以和虛擬環(huán)境放在同一目錄,也可以放在虛擬環(huán)境的文件夾里,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2022-09-09