import requests
import re
import openpyxl
from openpyxl.styles import Font
from openpyxl.styles import colors
def parse_html(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
}
response = requests.get(url, headers=headers)
text = response.text
regix = '<div class="pic">.*?<em class="">(.*?)</em>.*?div class="info.*?class="hd"' \
'.*?class="title">(.*?)</span>.*?class="other">' \
'(.*?)</span>.*?<div class="bd">.*?<p class="">(.*?)<br>(.*?)</p>' \
'.*?class="star.*?<span class="(.*?)"></span>.*?' \
'span class="rating_num".*?average">(.*?)</span>' \
'.*?<span>(.*?)</span>.*?<span class="inq">(.*?)</span>'
results = re.findall(regix, text, re.S)
datalistFather = []
firstLine = ['排名', '电影名称', '导演', '演员', '年份',
'国家', '类型', '星数', '评分', '评价数量', '简介']
datalistFather.append(firstLine)
for item in results:
datalistSon = []
daoyan = ''
yanyuan = ''
titleCn = item[1]
person = re.sub(' ', '', item[3].strip())
persons = person.split("主演:")
if len(persons) > 1:
daoyan = persons[0].replace('导演:', '')
yanyuan = persons[
Python爬取豆瓣电影Top25并存储至Excel表格
最新推荐文章于 2021-07-23 16:50:15 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)