First, write first

Really, why do other people send games so many people see, I sent twice a total of 100.

Forget it, not the whole game, anyway you also don’t like watching ~

Today, I’m going to try to climb the hot girls in the headlines. I don’t know if I can stand it

Second, preparation

1. The environment of use
  • Python 3.8
  • Pycharm 2021.2 Pro
2. Third-party modules to be used
  • selenium
  • requests
  • parsel

Iii. General process

You don’t like me to talk too much, but the process, I’m going to write it out for you, so I’m going to separate it out.

1. Website analysis (clear requirements)
  1. Find the link to embedUrl in the video page source code;
  2. Find the video playing address in the link, in the elements panel;
  3. The groupBy_id on the embedUrl was the ID on the current video link, so you only needed an ID to download the video.
2. Code implementation process
  1. Build embedUrl
  2. Use Selenium to access this link
  3. Extract the video link address
  4. Splice video link address
  5. Use Requests to send requests and retrieve video binary data
  6. Save the video

Iv. Code display and analysis

First import the module

import requests
from selenium import webdriver
Copy the code

Enter browser Settings

options = webdriver.ChromeOptions()
Copy the code

1. Build embedUrl

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

Headless browser

options.add_argument("--headless")
Copy the code

Add a disguise

Options. Add_argument (' the 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"')Copy the code

2. Use Selenium to access the link driver: browser

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

Open a web-driven configuration: code to operate a browser in the middle

driver.get(url)
Copy the code

Implicit wait: wait up to five seconds if the load finishes in one second to continue execution

driver.implicitly_wait(5)
Copy the code

3. Extract the 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")
Copy the code

4. Splicing video link address

video_url = 'http:' + video_url
Copy the code

5. Use Requests to send requests and get video binary data

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

V. All codes

Import requests from Selenium import webdriver # set options = webdriver.chromeOptions () # 1. Build embedUrl https://www.ixigua.com/embed?group_id=7029910152576926238 group_id = input (" please enter id: do you want to download the video ") url = 'https://www.ixigua.com/embed?group_id=' + group_id # headless browser options. Add_argument (" headless ") # add a disguise Options. Add_argument (' the 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 to access this link # driver: Browser driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options) Driver.get (URL) # Implicit wait: wait up to five seconds if the load is finished continue to execute driver. Implicitly_wait (5) # 3. Extract the url 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. Get (video_url).content with open('1.mp4', mode='wb') as f: F.rite (video_data) print(" climb success!!" Let's see if you're smart enough to find itCopy the code