import urllib.request
from bs4 import BeautifulSoup
import re
import xlwt
baseurl="https://maoyan.com/board/4?offset="
datalist=[]
findtitle=re.compile('img alt="(.*?)" class="board-img"')
findtplj=re.compile(r'class="board-img" data-src="(.*?)"/>')
findstar=re.compile(r'<p class="star">(.*?)</p>',re.S)
findreleasetime=re.compile('<p class="releasetime">(.*?)</p>')
findlink=re.compile(r'href="(.*?)"')
def links(url):
header={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
}
link=urllib.request.Request(url=url,headers=header)
req=urllib.request.urlopen(link)
xinxi=req.read().decode("utf-8")
xinxi1=BeautifulSoup(xinxi,"html.parser")
#print(xinxi1)
xinxi2=xinxi1.find_all("dd")
#print(xinxi2)
for i in xinxi2:
data=[]
i=str(i)
title=re.findall(findtitle,i)
tulj=re.findall(findtplj,i)
star=re.findall(findstar,i)
star=[x.strip() for x in star if x.strip()!='']
releasetime =re.findall(findreleasetime,i)
link=re.findall(findlink,i)[0]
links="https://maoyan.com"+link
print(links)
data.append(title)
data.append(tulj)
data.append(star)
data.append(releasetime)
data.append(links)
datalist.append(data)
return datalist
workbook=xlwt.Workbook(encoding="utf-8")
sheet=workbook.add_sheet("猫眼TOP100")
col=["电影名","电影海报连接","演员表","上映时间","电影连接"]
for i in range(0,5):
sheet.write(0,i,col[i])
for i in range(0,10):
url=baseurl+str(i*10)
links(url)
for i in range(0,100):
data=datalist[i]
for j in range(0,5):
sheet.write(i+1,j,data[j])
workbook.save("猫眼电影top100.xls")
python爬取猫眼电影top100信息并保存在表格中
最新推荐文章于 2022-07-11 16:57:26 发布