requests_spider.py
import requests
import json
def get_html_text(url,headers=None):
if None == headers:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"}
response = requests.get(url,headers=headers)
response.encoding = "utf-8"
return response.text
def get_json(url,headers=None):
return json.loads(get_html_text(url,headers))
def get_image(url,headers=None):
if None == headers:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"}
return requests.get(url,headers=headers).content
writer.py
def write_to_image(path, content):
with open(path, "wb") as f:
f.write(content)
bilibilimath.py
from requests_spider import get_json,get_image
from writer import write_to_image
import urllib.parse
if __name__ == "__main__":
args = {
"tag":"今日热门",
"page_size":50,
"next_offset":0,
"platform":"pc"
}
url="https://api.vc.bilibili.com/board/v1/ranking/top?"+urllib.parse.urlencode(args)
obj = get_json(url)
headers = {
"Origin": "https://www.bilibili.com",
"Referer": "https://www.bilibili.com/video/av10978955",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"}
for item in obj["data"]["items"]:
print(item["item"]["video_playurl"])
write_to_image("{}.mp4".format(item["item"]["id"]), get_image(item["item"]["video_playurl"],headers))