Python realizes automatic login of a certain sound + acquisition of video data

Preface

Dy must have been used by everyone, and it is often used, so today I will use the code to obtain its video data

The environment uses
Python 3.8
Pycharm

The module uses
requests
selenium
json
re

1. Data source analysis

1. Clarify the requirements
and clearly collect the website and data content

网址: https://www.dy.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ?modal_id=7270865943398518050

Data: Video link/Video title
2. Packet capture analysis
Perform packet capture analysis through developer tools
I. Open the developer tools: F12
II. Refresh the web page
III. Find the data link

视频链接: https://v26-web.dyvod.com/295eea512e6f187309e6181297ec185e/64e8a7f8/video/tos/cn/tos-cn-ve-15c001-alinc2/o8vKACOD9NSbaA3mnggzfIO5QAgkqHnGr7sAeB/?a=6383&ch=26&cr=3&dr=0&lr=all&cd=0%7C0%7C0%7C3&cv=1&br=609&bt=609&cs=2&ds=3&ft=bvTKJbQQqU-mfJ4ZPo0OW_EklpPiXV8zNMVJEdBqSpvPD-I&mime_type=video_mp4&qs=15&rc=NTg8NzpoNGY2aGU0N2k1PEBpajhuNTY6ZmhtbTMzNGkzM0AtMy4xY2E0Xi4xYDNjX15iYSM2bl5scjRvLWdgLS1kLWFzcw%3D%3D&btag=e00010000&dy_q=1692965337&l=20230825200856A1A3326D295C25055965

IV. By keyword search, find the data package corresponding to the link
video link/title --> from the source code of the web page <encoding>

2. Code implementation steps

  1. Send a request, simulate the browser to send a request for the url address
  2. Get data, get the response data returned by the server
  3. Analyze the data and extract the data content we need
    4. Save the data and save the video data

Code

send request

Simulated browser: <Can be copied directly> response.text Gets the response text data response.json() Gets the response json data
response.content Gets the response binary data
We use the requests.get() method to send a GET request to the specified URL and obtain to the content of the response

headers = {
    
    
    # User-Agent 用户代理, 表示浏览器基本身份信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}
# 请求链接
url = 'https://www.dy.com/user/MS4wLjABAAAArgJe6h-DzQcvyZ1O71yXSdJFn19Tqq8lFCIffgy5SlhwYlkseK5aM5ETF8KoaGDK?modal_id=7270476649714421046&vid=7269532986553552140'
# 发送请求
response = requests.get(url=url, headers=headers)

Analytical data

Use regular expressions to search and match specific patterns in HTML content to extract the desired data. Call the findall method in the re module re.findall('data: the
data you need', 'data source: where to get the data') --> find all the data content

# 获取响应文本数据  获取网页源代码内容
html_data = response.text
# 提取标题
title = re.findall('video_title" content="(.*?)"/>', html_data)[0]
# 提取视频信息 <经过了编码>
video_info = re.findall('<script id="RENDER_DATA" type="application/json">(.*?)</script>', html_data)[0]
# 解码
info = requests.utils.unquote(video_info)
# 把完整json数据格式字符串, 转成字典数据类型
json_data = json.loads(info)
# 根据冒号左边的内容[键], 提取冒号右边的内容[值]
video_url = 'https:' + json_data['app']['videoDetail']['video']['bitRateList'][0]['playAddr'][0]['src']

save data

Send request for video link, get binary data content, save local folder

video_content = requests.get(url=video_url, headers=headers).content
with open('video\\' + title + '.mp4', mode='wb') as f:
    f.write(video_content)
print(title)
print(video_url)

Simulated login

Import required modules

# 自动化测试模块
from selenium import webdriver
# demo
from chaojiying import Chaojiying_Client
from password import account, password
# 动作链
from selenium.webdriver.common.action_chains import ActionChains

Automatically log in to the browser

# 打开浏览器, 访问网站
driver = webdriver.Chrome()
driver.get('https://www.dy.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ')
# 延时
driver.implicitly_wait(10)
time.sleep(2)
# 获取验证码图片
img_label = driver.find_element_by_css_selector('.captcha_verify_container')
# 截图 保存验证码图片
img_label.screenshot('yzm.png')

transfer
Insert image description here

# 调用 --> 帮助我们识别文字坐标
chaojiying = Chaojiying_Client(账号, 密码, '96001')
# 读取图片
im = open('yzm.png', 'rb').read()
result = chaojiying.PostPic(im, '9004')['pic_str']
for res in result.split('|'):
    x = res.split(',')[0]
    y = res.split(',')[-1]
    ActionChains(driver).move_to_element_with_offset(img_label, int(x), int(y)).click().perform()

driver.find_element_by_css_selector('.captcha_verify_action div:last-of-type').click()
time.sleep(2)driver.implicitly_wait(10)
lis = driver.find_elements_by_class_name('Eie04v01')
for li in lis:
    video_id = li.find_element_by_css_selector('a').get_attribute('href').split('/')[-1]

Display of the final code execution results

Insert image description here

Guess you like

Origin blog.csdn.net/XM67_/article/details/132580697