编程语言
首页 > 编程语言> > python爬虫

python爬虫

作者:互联网


import os
from string import punctuation

import requests

import re
import pymysql
from moviepy.editor import VideoFileClip, AudioFileClip
from lxml import etree
import json
import time

os.chdir(r'E:\result')
headers_bv = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
valink = re.compile(r'<script>(.*?)</script>')
# 拿到window.__playinfo__后面的内容
infoink = re.compile(r'"author":"(.*?)".*?'
r'"bvid":"(.*?)".*?'
)


# 这个是为了拿到视频的名字 可以选择不用



data = {
'accept_description': '高清 1080P+',
'accept_quality': 112
}

url = 'https://search.bilibili.com/all?vt=65873071&keyword=%E8%B6%A3%E5%91%B3%E7%A7%91%E6%99%AE%E5%8C%96%E5%AD%A6%E5%AE%9E%E9%AA%8C&from_source=webtop_search&spm_id_from=333.1007&page=10&o=270'
html = requests.get(url).text

info = str(re.findall(valink, html))

info2 = re.findall(infoink, info)


conn = pymysql.connect(host='localhost', user='root', password='123456', database='exercise', charset='utf8',
port=3306)
# 创建游标对象
cursor = conn.cursor()
sql = 'INSERT INTO bilibili(`adress`, `author`) values(%s,%s)'

for it in info2:
try:

data = ["https://www.bilibili.com/video/"+it[1]+"?spm_id_from=333.337.search-card.all.click",it[0]]
cursor.execute(sql, data)
conn.commit()
except Exception as e:

print(e)



# 关闭游标
cursor.close()
# 关闭连接
conn.close()

print('over!!!')


标签:bilibili,re,python,爬虫,cursor,E5%,import,conn
来源: https://www.cnblogs.com/crimsonyu/p/16376190.html