初次完成爬取豆瓣电影top10
本帖最后由 Clearpython 于 2020-6-22 15:58 编辑import urllib.request,urllib.parse
import re
import xlwt
url = "https://movie.douban.com/top250" #定义一个网址
# 获取网页的函数
def get_data():
header = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
}
req = urllib.request.Request(url=url,headers=header)
r = urllib.request.urlopen(req)
html = r.read().decode("utf-8")
return html
# 分析字符串的函数
def analy_data():
douban_list = []
html = get_data() #前面定义的html已经回收了,要重新定义
movie_links = re.findall('<a href="(.*?)" class="">',html)
movie_names = re.findall('<span class="title">(.*?)</span>',html)
i = 1
for i in range(1,11):
douban_list.append(movie_names)
douban_list.append(movie_links)
i += 1
return movie_links,movie_names
def save_data():
movie_names,movie_links = analy_data()
workbook = xlwt.Workbook(encoding="utf8")
sheet = workbook.add_sheet("sheet1")
sheet.write(0,0,"movie_link")
sheet.write(0,1,"movie_name")
for i in range(1,11):
sheet.write(i,0,movie_names)
sheet.write(i,1,movie_links)
workbook.save("豆瓣电影top10.xls")
if __name__ == '__main__':
get_data()
analy_data()
save_data()
页:
[1]