爬虫记录

尝试爬取某网站音频,但是由于该网站防护较好,没有爬取成功,记录如下

code

from turtle import pen
import requests
import re
import os

#预处理
hea = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'}

#创建下载目录
if not os.path.exists('download'):
   os.makedirs('download')

curPath = os.getcwd() #当前路径
print(f'curPath:{curPath}')


#请求网页
curUrl = 'http://zaoaoaoaoao.com/program/20150818'
html = requests.get(curUrl,headers = hea)
html.encoding = 'utf-8'
webhtml = html.text #网页源代码

curDir = curUrl.split('/')[-1] #当前网页下载目录
print(f'curDir:{curDir}')

#创建当前网页下载目录
curDownloadDir = curPath + "\\download\\" + curDir
print(f'ddir:{curDownloadDir}')
if not os.path.exists(curDownloadDir):
   os.makedirs(curDownloadDir)

#写入文件操作,可忽略
# file = open('a.txt','w', encoding='utf-8')
# file.write(webhtml)
# file.close()

flist = re.findall(r'http://audio.zaoaoaoaoao.com/.{0,40}mp3',webhtml)
print(f'len:{len(flist)}')
for url in flist:
   #文件名
   audioName = url.split('/')[-1]
   print(audioName)
   print(url)

反思

  • 上面代码简单包含爬取一个简单网站的必要步骤

  • 但是由于该音频网站不能下载,虽然最后能够拿到url链接,但是403,无权限,翻车了

  • 不过偶尔能够侥幸打开筛选的音频链接,无法直接下载,但是可以通过另存为下载,但是这已经超出我的理解范围了,,,好在现在网站给了百度云链接,但是音频好多啊...

posted on 2022-01-20 17:18  彼岸花不开彼岸  阅读(110)  评论(0)    收藏  举报