欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

快手批量获取真实地址python

程序员文章站 2022-04-16 21:25:44
...
import requests
from bs4 import BeautifulSoup
import re
import xlrd
import xlwt

class KSNoMark():
    #  忽略警告代码
    requests.packages.urllib3.disable_warnings()

    def GetShareUrl(self):
        try:
            # 打开Excel表格
            wb = xlrd.open_workbook('分享链接.xlsx')
            # 获取当前正在显示的sheet
            sheet = wb.sheet_by_name('url')

            urls = [(sheet.cell_value(i, 0))for i in range(1, sheet.nrows)]

            while '' in urls:
                urls.remove('')
        except FileNotFoundError:
            print("links.xlsx文件不存在")
        return urls

    def GetRealUrl(self):

        headers = {
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
        }

        urls = self.GetShareUrl()

        # 创建一个workbook设置编码
        workbook = xlwt.Workbook(encoding='utf-8')
        # 创建一个worksheet
        worksheet = workbook.add_sheet('realLinks')
        row_num = 0

        for url in urls:
            worksheet.write(row_num, 0, url)
            response = requests.get(url, headers=headers, allow_redirects=False,verify=False)
            share_url = response.headers['Location']

            share_response = requests.get(share_url,headers=headers,verify=False).text

            # 通过BeautifulSoup提取无水印播放地址字符串
            soup = BeautifulSoup(share_response,'lxml')
            noWaterMarkVideo = soup.find(attrs={'id': 'hide-pagedata'}).attrs['data-pagedata']

            try:
                # 正则处理字符串获取真实地址
                pattern = re.compile('\"srcNoMark\":"(.*?)"},',re.S)
                real_url = re.findall(pattern,noWaterMarkVideo)[0]
                worksheet.write(row_num, 1, real_url)
            except Exception as e:
                print(e)
                pass

            row_num += 1

            # 保存
            workbook.save('快手视频真实地址.xls')
            continue

if __name__ == '__main__':
    KSNoMark = KSNoMark()
    KSNoMark.GetRealUrl()
    print("快手真实地址采集完成")

快手批量获取真实地址python

表格名称:分享链接.xlsx

sheet名称:url

第一列第一行:分享链接

第一列 第2-n行:需要解析的分享视频地址

相关标签: python