一、寫在前面
真的,為什么別人發游戲這么多人看,我發了兩次了加起來才一百個。
算了算了,不整游戲了,反正你們也不愛看~
今天來試試把頭條上扭腰上熱門的那些妹子爬一爬,不知道我頂不頂得住~
二、準備工作 1、使用的環境 python 3.8pycharm 2021.2 專業版 2、要用的第三方模塊 seleniumrequestsparsel 三、大致流程
鑒于你們不喜歡我啰嗦,但是流程呢,我還是要給你們寫出來,所以我就單獨把它列出來了。
1、網站分析(明確需求) 在視頻網頁源代碼當中找到 embedUrl 對應的鏈接;在鏈接當中找到視頻播放地址,在元素面板當中;發現規律 embedUrl上面的 groupby_id 其實就是當前視頻鏈接上的id,下載視頻的時候 就只需要 一個 id 就可以下載視頻;(https://www.ixigua.com/embed?group_id=7029910152576926238) 2、代碼實現過程 構建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238使用selenium訪問該鏈接提取視頻鏈接地址拼接視頻鏈接地址使用requests發送請求 并且獲取視頻二進制數據保存視頻
如果大家在學習Python的過程中不知道學習方向,該怎么學,沒有好的系統的學習資料、沒人交流解答等等,都可以私我,我都給大家準備好了。
四、代碼展示分析
首先導入一下模塊
import requestsfrom selenium import webdriver
進入瀏覽器設置
options = webdriver.ChromeOptions()
1、構建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238
group_id = input("請輸入你要下載視頻的id:")url = 'https://www.ixigua.com/embed?group_id=' + group_id
無頭瀏覽器
options.add_argument("--headless")
加一個偽裝
options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"')
2、使用selenium訪問該鏈接 driver: 瀏覽器
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
打開一個網頁驅動配置: 代碼操作瀏覽器的一個中間人
driver.get(url)
隱式等待: 最多等待五秒 如果一秒鐘加載完了 繼續執行
driver.implicitly_wait(5)
3、提取視頻鏈接地址
info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')video_url = info[0].get_attribute("url")
4、拼接視頻鏈接地址
video_url = 'http:' + video_url
5、使用requests發送請求 并且獲取視頻二進制數據
video_data = requests.get(video_url).contentwith open('1.mp4', mode='wb') as f: f.write(video_data)
所有代碼
import requestsfrom selenium import webdriver# 進入瀏覽器設置options = webdriver.ChromeOptions()# 1. 構建embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238group_id = input("請輸入你要下載視頻的id:")url = 'https://www.ixigua.com/embed?group_id=' + group_id# 無頭瀏覽器options.add_argument("--headless")# 加一個偽裝options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"')# 2. 使用selenium訪問該鏈接# driver: 瀏覽器driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)# 打開一個網頁# 驅動配置: 代碼操作瀏覽器的一個中間人driver.get(url)# 隱式等待: 最多等待五秒 如果一秒鐘加載完了 繼續執行driver.implicitly_wait(5)# 3. 提取視頻鏈接地址info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')video_url = info[0].get_attribute("url")# 4. 拼接視頻鏈接地址video_url = 'http:' + video_url# 5. 使用requests發送請求 并且獲取視頻二進制數據video_data = requests.get(video_url).contentwith open('1.mp4', mode='wb') as f: f.write(video_data)print("爬取成功!!!")#留了報錯,看看大家夠不夠機智找出來
兄弟們看完覺得有幫助,記得點贊三連哇~
到此這篇關于Python 超簡潔且詳細爬取西瓜視頻案例的文章就介紹到這了,更多相關Python 西瓜視頻內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/fei347795790/article/details/121477677