爬 豆瓣 喜马拉雅
1 爬 豆瓣电影排行榜
from bs4 import BeautifulSoup from lxml import html import xml import requests url = "https://movie.douban.com/chart" f = requests.get(url) # Get该网页从而获取该html内容 soup = BeautifulSoup(f.text, "lxml") # 用lxml解析器解析该网页的内容, 好像f.text也是返回的html #因为calss和关键字冲突,所以改名class_ for k in soup.find_all('div',class_='pl2'): # 找到div并且class为pl2的标签 name = k.find_all('a')[0] # 在每个对应div标签下找span标签,会发现,一个a里面有四组span # res = name.get('href') res = name.get_text().split('/')[0].strip() print(res)
2 爬 喜马拉雅 音乐/图片
# 爬取 简单加密的东西 图片/音乐 # 手动模拟请求头 Header信息 浏览器代理 # http请求头的验证 反爬机制之一 现在是程序访问 User-Agent import requests from uuid import uuid4 import time # res = requests.get('') # sort 顺序 pagesize30 每页 url_str = "https://www.ximalaya.com/revision/play/album?albumId=%s&pageNum=%s&sort=-1&pageSize=30" #link header = { 'User-Agent':"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" } my_url = url_str%("424529","1") # 字符串格式化/替换 res = requests.get(my_url,headers=header) # 200 状态 # print(res.content) # 内容 data = res.json() # 字典 print(data.get('ret')) # 状态 print(data.get('data').get('tracksAudioPlay')[20].get('trackName')) # 状态 for music_info in data.get("data").get("tracksAudioPlay"): audio = requests.get(music_info.get('src')) with open(f'{uuid4()}.mp3','wb') as f: f.write(audio.content) time.sleep(0.3)