使用Python爬取豆瓣top250的相关数据,并保存到同目录下Excel
import re
import urllib.error
import urllib.request
import xlwt
from bs4 import BeautifulSoup
def main():
baseurl ="https://movie.douban.com/top250?start=/"
datelist = getDate(baseurl)
savepath=".\douban.xls"
saveDate(datelist,savepath)
# askURL("https://movie.douban.com/")
findlink = re.compile(r'')
findimg = re.compile(r',re.S)
findtitle = re.compile(r'(.*)"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"}
request=urllib.request.Request(url,headers=head)
html=""
try:
response=urllib.request.urlopen(request)
html=response.read().decode("utf-8")
# print(html)
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
def saveDate(datalist,savepath):
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('豆瓣电影',cell_overwrite_ok=True)
col =("电影详情","图片","影片","评分","评价数","概况")
for i in range(0,5):
worksheet.write(0,i,col[i])
for i in range(0,250):
print("第%d条" %(i+1))
data=datalist[i]
for j in range(0,5):
worksheet.write(i+1,j,data[j])
workbook.save(savepath)
if __name__ == '__main__':
main()
print("爬取完毕")
直接复制粘贴就行。
若要更改爬取网站,则需要更改URL以及相应的html格式(代码中的“item”)



