res = requests.get(url=url, headers=headers)
response = res.content.decode(“utf-8”)
#电影名称
movie_title=html.xpath(“//div[@class=‘hd’]//a//span[1]//text()”)
#电影评分
movie_score=html.xpath(‘//div[@class=“star”]//span[2]//text()’)
#电影排名
movie_degree=html.xpath(‘//div[@class=“item”]//div//em//text()’)
#电影海报地址
movie_poster=html.xpath(‘//div[@class=“pic”]//a//img/@src’)
#导演,使用正则
movie_director = re.findall(r"导演:(.*?);", response)
a = “”.join(movie_director).split(“ ”)#因为使用的正则抓取下来的字符串含有 ,使用split()函数进行切分
movie_director = a[:25]
if movie_director==“”:
movie_director=“”
else:
movie_director=movie_director
#主演,使用正则
movie_main_act = re.findall(“主演: (.*)
”, response)
movie_main_act = “”.join(movie_main_act).split(“…”)
if movie_main_act==“”:
movie_main_act=“”
else:
movie_main_act=movie_main_act
#上映日期
movie_datatime= re.findall(r"(\d*) ", response)
b = “”.join(movie_datatime)#转化为字符串
movie_datatime = [b[i:i + 4] for i in range(0, len(b), 4)]#转换的字符串是一对数字如199419931994…,所以进行切分 4个一切就是年份
if movie_datatime==“”:
movie_datatime=“”
else:
mo