废话不多说直接上代码
"""
from selenium import webdriver
# 打开一个浏览器
driver = webdriver.Chrome()
# 访问网址
driver.get('https://www.douyin.com/user/MS4wLjABAAAAkzRSrOuSsM4Z1Ricsddumx_aSvX0jmOPcQR2qTs3PEtImBD8BomLrqvtIOBKOL0P')
def drop_down():
for x in range(1, 30, 2):
time.sleep(1)
j = x / 9
js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
driver.execute_script(js)
drop_down()
# 通过css选择去定位元素 ---> 找标签
lis = driver.find_elements_by_css_selector('.ECMy_Zdt')
# for循环
for li in lis:
try:
url = li.find_element_by_css_selector('a').get_attribute('href')
"""
1. 发送请求, 模拟浏览器对于url地址发送请求
需要注意什么细节:
表示是请求成功, 但是不代表你得到数据
"""
# 确定url地址
# url = 'https://www.douyin.com/video/7064550745437146383'
# 模拟浏览器 伪装python代码 设置请求头参数
headers = {
# 无论是否有登陆 都有 cookie
'cookie': 'douyin.com; ttwid=1%7CHj5s1edW817SdFa5U5hnifjpPs-xPs4Lv7vC7DQNm60%7C1652797030%7C21e300be59bef00949a7d8f790beeeb4db77670205394cc7cce9dcd0229b5e95; odin_tt=fded742fd6268e282962a1c63dd4f62e37e3bd8950387213e5a8bf5d86daef284ad8b9c31fb69e43d70253eca40fdb3fd0dad5ea27c7288dc9f4910ffcd7cce1; s_v_web_id=verify_l5rpgfoe_2FbyEP85_x7NT_4lnS_8mmC_nBHk2mpOAmBm; passport_csrf_token=512069ecb5db0a00d52dbef10af4dc80; passport_csrf_token_default=512069ecb5db0a00d52dbef10af4dc80; strategyABtestKey=1659508208.59; download_guide=%221%2F20220803%22; THEME_STAY_TIME=%22299791%22; IS_HIDE_THEME_CHANGE=%221%22; __ac_nonce=062ea61cf0015449b429c; __ac_signature=_02B4Z6wo00f01qBh60AAAIDCIGMRAcbkcOKgQe.AAMr-cutAXyUweCACuMTMehP6MqIdwv2UEAokF6bgwnKJHr-hyQGeTR3ihhKs3ZnrQMVRw201.2SFchUf9IYC773W0pl8PgdRzDjOMuyxe4; douyin.com; home_can_add_dy_2_desktop=%221%22; tt_scid=JJ6iYRr.ZdTE5Je0lI2iTvxgISufJnQJCWA52nAk039mUVI2M0tFOAEhXUTbS5dR4460; msToken=YBz-BW7m8hhFhPyQMrwofvkqhXmfcY5CZz5CWtGWgAklgELhEww4OWk067p3bI2IksUw7vbX7uKG2jb1niFtISXpdv-vue9YY6lBHXOIkPQdQg9oJVeQs2s=; msToken=B66A2vCUlfhY3aX_Vf9_z5Lk-SME5-nNGQXkKPOAJM42SYQsWlg9qUtM2Hr6xw4rZYpkGhO7yzt92WXL4nJ3FU2EbimiozujrVjWw-6IWDFLWVfM9np7oSw=',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
# 发送请求 左边url是get请求方法里面形式参数 右边url是我们自定义变量, 传入进去参数
response = requests.get(url=url, headers=headers)
# 打印响应对象
print(response)
# 2. 获取数据 print(response.text)
"""
3. 解析数据 ---> 根据返回数据类型<内容> 以及 你想要数据内容 选择最方便合适解析方法
re正则表示式 直接可以对于字符串数据进行提取
除非说我们用re提取说出来之后, 是json数据格式, 然后通过数据类型转换之后, 再用字典取值
"""
# 提取标题
title = re.findall('(.*?) ', response.text)[0]
#
html_data = re.findall(' requests简单使用 方法 系统课程都教授了
# requests.utils.unquote 解码 requests.utils.quote 编码
html_data = requests.utils.unquote(html_data)
# 转换数据类型
json_data = json.loads(html_data)
# 字典取值 字典取值, 根据冒号左边的内容, 提取冒号右边的内容
video_url = 'https:' + json_data['75']['aweme']['detail']['video']['bitRateList'][0]['playAddr'][0]['src']
print(title)
print(video_url)
# 保存数据
video_content = requests.get(url=video_url, headers=headers).content
with open('video\\' + title + '.mp4', mode='wb') as f:
f.write(video_content)
except Exception as e:
print(e)
如果需要更多代码可以在b站搜索诣秋