Skip to content

Ziazan/douyin_web

Repository files navigation

记录:(signature的规则更新了,无法获得视频列表数据)

  1. 使用selenium 生成的sign 和真实的sign有区别
  2. 怀疑是加密的js里面判断了webdriver,所以改用 firfox的webdriver ----失败
  3. 既然是需要js生成signature 就在python中引入PyExecJS ----失败 js中一些变量获取不到
  4. 使用pyppeteer ------ 失败 依然被识别

通过抖音的分享页抓取视频信息

目标

通过抖音用户主页的分享链接例如:https://v.douyin.com/KhkbCq/ 获取用户的基本信息,如:粉丝数/视频数/视频评论量/视频发布时间/视频点赞数

思路记录

访问分享链接之后,获取用户的基本信息。抖音id/昵称/点赞数/关注数/粉丝数 分析视频列表接口的规则,生成视频列表访问的url,此url中的signature 需要生成一个html文件,selenium打开html文件能获取。(见 signature分析.md文件)

在视频列表接口返回的json中 可以拿到 视频的基本信息和视频播放地址 再用requests访问视频下载地址,下载视频到本地。 此项目使用mongodb 存储数据

项目文件说明:

方式一(未完成)

  1. 读需要爬取的抖音用户的分享页链接写在share_task.txt
  2. 直接运行 run.py 文件

方式二

  1. 读需要爬取的抖音用户的分享页链接写在share_task.txt
  2. 运行 handle_share.py 获取share_task.txt配置的抖音用户的基本信息 点赞数/关注数/粉丝数
  3. 运行 video_list_url.py获取用户的视频列表信息: 点赞数/关注数/转发数/评论数
  4. 运行video_download.py下载指定用户的所有无水印视频保存到video文件夹

运行截图

https://github.com/Ziazan/douyin_web/blob/master/doc/img/user_info.png

https://github.com/Ziazan/douyin_web/blob/master/doc/img/download_video.png

https://github.com/Ziazan/douyin_web/blob/master/doc/img/video_lsit.png

视频链接获取的 signature 分析

signature分析.md文件

遇到的报错

Q:Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

A: 使用 ChromeDriverManager 去下载对应chrome版本的 ChromeDriver

pip install webdriver-manager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

参考:https://stackoverflow.com/questions/29858752/error-message-chromedriver-executable-needs-to-be-available-in-the-path

Q: signature.html 总是不能正确拿到视频列表的接口url https://github.com/Ziazan/douyin_web/blob/master/doc/img/error1.png

A:使用selenium 和正常打开的浏览器生成的sigenature不一样。 有可能是 在js代码中判别了浏览器的原因。 如何突破网站对selenium的屏蔽

方法一:想到js中删除相关关键词,但是js代码都混淆加密过了。不可行

方法二:把selenium浏览器伪装成真实浏览器,还是和真是signature有出入不可行

如何正确移除Selenium中window.navigator.webdriver的值

from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = Chrome(options=option)

使用这个方法,生成的signature selenium 中生成的结果: Q94FRRAeHXEwKA8qaryWr0PeAV

正常浏览器的结果: Q94FRRAeHXEwKA8qaryWr0PeBV

目前只剩倒数第二位的数值是相差1的结果。 见video_list_url.pyget_video_list_url()方法

Q:urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=62785): Max retries exceeded with url: /session/8a9ff6e4be66e9833b0a16750c5fe67e/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x110acd7c0>: Failed to establish a new connection: [Errno 61] Connection refused')) A:....好像ip被封了。要找代理了

Q:request 配置代理的时候报错:urllib3.exceptions.ProxySchemeUnknown: Not supported proxy scheme None A:

 proxies = { 
        "http":'http://' + ip_list.get_http_ip(),
        "https": 'https://' +ip_list.get_https_ip()}

格式需要是 http:// + ip + :端口

Q:Message: 'chromedriver' executable needs to be in PATH A: windows.解决方法 mac 解决方法

参考

  1. xpath helper 插件
  2. 在线字体编辑器
  3. Python爬虫如何获取重定向后的url
  4. 2020抖音无水印视频解析真实地址
  5. Python selenium 模拟Chrome浏览器打开手机模式
  6. (最新版)如何正确移除Selenium中的 window.navigator.webdriver

About

抖音用户分享页数据爬虫

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published