python3 爬取乐谱
生活随笔
收集整理的這篇文章主要介紹了
python3 爬取乐谱
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近感覺找樂譜下載之類比較麻煩, 于是花了兩天時間寫了個爬蟲程序, 效率倍增。
使用方法: 在https://www.17jita.com上搜索樂譜,點開第一個界面,將網(wǎng)址按程序內格式賦給url_sa下,然后運行,便可以在D:/pic/目錄下獲得樂譜。
效果如下:
源碼(日后有時間會繼續(xù)完善):
import urllib.request import re import os from bs4 import BeautifulSoup#database recipeWeb = "https://www.17jita.com/" recipeWeb_len = len(recipeWeb)webList = [] FinalList = []url_sa = "tab/img/4289" url = recipeWeb + url_sa + '.html'def getList(url):data = getData(url)pre = r'href='post = r'.*?html'webList = findPreAndPost(pre, post , data) return webListdef findPreAndPost(pre, post, data):loc = []res = []length = re.split(r'\s', data)for i in length:if(re.match(pre,i)):loc.append(i)for i in loc:if(re.match(post,i)):res.append(i)return resdef getData(url):webPage = urllib.request.urlopen(url)data = webPage.read()data = data.decode('gbk')return datadef GetPic(url, cnt):data = getData(url)soup = BeautifulSoup(data, 'html.parser')length = re.split(r'\s', data)title = soup.find('h1', {'class': 'ph'}).textloc = []res = []for i in length:if(re.match(r'src=',i)):loc.append(i)for i in loc:if(re.match(r'.*?png',i)):res.append(i)for pic in res:tmp = re.search(r'src="(.*?)"',pic)pic_url = tmp.group(1)print(pic_url)if(pic_url[0]=='/' or pic_url[0]!='h'): continue;pic_web = urllib.request.urlopen(pic_url)pic_data = pic_web.read()path = "d:/pic/"+title+'/';if not os.path.exists(path):os.makedirs(path)f = open(path+title+str(cnt)+".png","wb")f.write(pic_data)f.close()print(pic)webList = getList(url) FinalList.append(url) for i in webList:cnt = 0searchStr = 'href="'+ url_sa + '(.*?)"'tmp = re.search(searchStr, i)if tmp:ans = tmp.group(1)W = recipeWeb+url_sa+ansif not W in FinalList:FinalList.append(W)cnt = 0 for web_url in FinalList:cnt += 1GetPic(web_url, cnt)總結
以上是生活随笔為你收集整理的python3 爬取乐谱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数钢筋
- 下一篇: Python Defensive Pro