高等数学与python高级应用_python高级应用程序与设计
# 用于獲取網(wǎng)頁源碼的函數(shù)
def getHTMLText(url):try:
# 使用requests庫爬取網(wǎng)頁源碼2
r= req.get(url, headers={'user-agent': 'Mozilla/5.0'})
# 判斷是否爬取過程中出現(xiàn)錯(cuò)誤
r.raise_for_status()
# 設(shè)置源碼的編碼格式
r.encoding=r.apparent_encodingreturnr.text
except:return "爬取失敗"# 拼接地址,返回爬取地址數(shù)組
def urlSplit(path, sheet):
array=[]for i in sheet["href"]:
url= path+i
array.append(url)returnarray
# 獲取指定url頁面中電視劇列表集合
def TvList(html):
# 外部傳來的全局?jǐn)?shù)組
Tvlist=[]
#使用BeautifulSoup類解析html文本
soup= BeautifulSoup(html, "html.parser")
#獲取每個(gè)電視劇的a鏈接數(shù)組
As= soup.select("ul.fed-list-info > li >a.fed-list-title")
#從a標(biāo)簽中獲取電視劇id,name,hreffor a inAs:
# 獲取href
href= a.attrs["href"]
# 獲取電視劇id
id= href.split('/')[-2]
# 獲取Tv name
name=a.text
tem=[id,name,href]
Tvlist.append(tem)returnTvlist
# 獲取電視劇詳情頁數(shù)據(jù)集合
def TvDetailList(html, TvDetaillist):
# 解析網(wǎng)頁源碼
soup= BeautifulSoup(html, "html.parser")
# 電視劇名稱
title= soup.h1.a.string# 電視劇地址
href= "https://www.bt5156.com"+soup.h1.a.attrs['href']
# 臨時(shí)數(shù)組
temp=[]for index, value in enumerate(soup.find_all('li', 'fed-part-eone')):if(index > 5):breaktemp.append(value.text)
# 主演
actor= temp[0].split(":")[1]
# 導(dǎo)演
director= temp[1].split(":")[1]
# 地區(qū)
sort= temp[2].split(":")[1]
# 地址
location= temp[3].split(":")[1]
# 年份
year= temp[4].split(":")[1]
# 最新修改時(shí)間
date= temp[5].split(":")[1]
# 判斷可能會(huì)出現(xiàn)錯(cuò)誤的地方,將錯(cuò)誤跳過try:
# 集數(shù)
number= soup.find_all('span', 'fed-list-remarks')[0].text
number= re.findall('\d+', number)[0]
except:
number= ''# 不論number數(shù)據(jù)是否錯(cuò)誤,都執(zhí)行以下代碼finally:
# 評(píng)分
score= soup.find_all('span', 'fed-list-score')[0].text
# 簡介
description= soup.find_all('div', 'fed-part-esan')[0].text
# 對(duì)數(shù)據(jù)進(jìn)行組裝,一個(gè)temp就是一個(gè)電視劇詳細(xì)信息
temp=[title, href, actor, director, sort,location, year, date, number, score, description]
# 將電視劇信息存入集合
TvDetaillist.append(temp)
print(title)
# 將數(shù)據(jù)保存到excel文件中
def saveExcel(writer,sheet,df):
# 將數(shù)據(jù)寫入excel表中
df.to_excel(writer, sheet_name=sheet)
def saveTvListToExcel(urls, pages, TvlistPath):
writer=pd.ExcelWriter(TvlistPath)
# 爬取網(wǎng)站前n頁電視劇列表,分別存儲(chǔ)在不同的工作簿中for index, url inenumerate(urls):
print(url+" "+pages[index])
# 網(wǎng)頁源碼
html=getHTMLText(url)
# 返回電視劇列表
Tvlist=TvList(html)
# 將列表轉(zhuǎn)為pandas中的DataFrame
df= pd.DataFrame(Tvlist, columns=['id', 'name', 'href'])
# 將DataFrame寫入excel中,數(shù)據(jù)持久化
saveExcel(writer, pages[index], df)
# 從內(nèi)存中寫入excel
writer.save()
def saveTvDetailListToExcel(path,pages, TvlistPath, TvDetaillistPath):
col= ["title", "href", "actor", "director", "sort", "location","year", "date", "number", "score", "description"]
writer=pd.ExcelWriter(TvDetaillistPath)for page inpages:
TVDetaillist=[]
sheet= pd.read_excel(TvlistPath, sheet_name=page)for url inurlSplit(path, sheet):
# 循環(huán)地址,爬取電視劇詳情頁
html=getHTMLText(url)try:
TvDetailList(html, TVDetaillist)
except:
# 發(fā)生錯(cuò)誤立即將數(shù)據(jù)保存至excel中并停止爬取
df= pd.DataFrame(TVDetaillist, columns=col)
saveExcel(writer, page, df)
writer.save()
print("有一條數(shù)據(jù)爬取錯(cuò)誤")return 0df= pd.DataFrame(TVDetaillist, columns=col)
saveExcel(writer, page, df)
writer.save()
# 爬取的頁數(shù)
def SpiderPageNum(n, urls, pages):for i in range(1, n+1):
url= "https://www.bt5156.com/vodshow/lianxuju--------" +\
str(i)+ "---/"urls.append(url)
page= "page" +str(i)
pages.append(page)
總結(jié)
以上是生活随笔為你收集整理的高等数学与python高级应用_python高级应用程序与设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习中为什么需要梯度下降_梯度下降直
- 下一篇: python解析原理_代码详解:Pyth