程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Ten year old Python programmer: this video is a little shaky

編輯:Python

One 、 Write it at the front

Really? , Why do so many people watch games , I sent it twice. It adds up to one Mimi …

Come on come on , Not the whole game , You don't like it anyway ~

Today, let's try to climb the hot girls in the headlines , I don't know if I can stand it ~

Two 、 preparation

1、 Environment used

  • python 3.8
  • pycharm 2021.2 pro

2、 Third party modules to be used

  • selenium
  • requests
  • parsel

3、 ... and 、 Approximate process

Since you don't like my wordiness , But what about the process , I'll still write it for you , So I listed it separately .

1、 Website analysis ( Clear requirements )

  • Found in the video web page source code embedUrl Corresponding links ;
  • Find the video playback address in the link , In the element panel ;
  • Found that regular embedUrl above groupby_id In fact, it is on the current video link id, When you download the video Just need One id
    You can download the video ;(https://www.ixigua.com/embed?group_id=7029910152576926238)

2、 Code implementation process

  • structure embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238
  • Use selenium Visit the link
  • Extract video link address
  • Splicing video link address
  • Use requests Send a request And get video binary data
  • Save the video

If you are learning Python In the process of learning, I don't know the learning direction , How to learn , There is no good system of learning materials 、 No one exchanges answers and so on , You can private me , I'm ready for everyone .

Four 、 Code presentation analysis (https://jq.qq.com/?_wv=1027&k=SFu7oNIZ)

First, import the module

import requests
from selenium import webdriver

Go to browser settings

options = webdriver.ChromeOptions()

1、 structure embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238

group_id = input(" Please enter the video you want to download id:")
url = 'https://www.ixigua.com/embed?group_id=' + group_id

Headless browser

options.add_argument("--headless")

Add a disguise

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、 Use selenium Visit the link
driver: browser

driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

Open a web page
Drive configuration : Code operates as a middleman in the browser

driver.get(url)

An implicit wait : Wait up to five seconds If the load is finished in one second Carry on

driver.implicitly_wait(5)

3、 Extract video link address

info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')
video_url = info[0].get_attribute("url")

4、 Splicing video link address

video_url = 'http:' + video_url

5、 Use requests Send a request And get video binary data

video_data = requests.get(video_url).content
with open('1.mp4', mode='wb') as f:
f.write(video_data)

All of the code (https://jq.qq.com/?_wv=1027&k=SFu7oNIZ)

python Answering question consulting Learning exchange group 2:660193417###
import requests
from selenium import webdriver
# Go to browser settings
options = webdriver.ChromeOptions()
# 1. structure embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238
group_id = input(" Please enter the video you want to download id:")
url = 'https://www.ixigua.com/embed?group_id=' + group_id
# Headless browser
options.add_argument("--headless")
# Add a disguise
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. Use selenium Visit the link
# driver: browser
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)
# Open a web page
# Drive configuration : Code operates as a middleman in the browser
driver.get(url)
# An implicit wait : Wait up to five seconds If the load is finished in one second Carry on
driver.implicitly_wait(5)
# 3. Extract video link address
info = driver.find_elements_by_xpath('//*[@id="player_default"]/xg-controls/xg-definition/ul/li[1]')
video_url = info[0].get_attribute("url")
# 4. Splicing video link address
video_url = 'http:' + video_url
# 5. Use requests Send a request And get video binary data
video_data = requests.get(video_url).content
with open('1.mp4', mode='wb') as f:
f.write(video_data)
print(" Climb to success !!!")
# Leave an error , See if everyone is smart enough to find out

Can you stand it, brothers ?( Manual formation )


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved