python爬取电影数据并分析(程序员大佬教你用python爬取电影天堂)
python爬取电影数据并分析(程序员大佬教你用python爬取电影天堂)def get_detail_urls(url): response = requests.get(url headers=HEADERS) # response.text 是系统自己默认判断。但很遗憾判断错误,导致乱码出现。我们可以采取另外方式 response.content。自己指定格式解码 # print(response.text) # print(response.content.decode('gbk')) # print(response.content.decode(encoding="gbk" errors="ignore")) text = response.content.decode(encoding="gbk" errors="igno
- 前言:
那么什么是爬虫呢?我们把互联网有价值的信息都比喻成大的蜘蛛网,而各个节点就是存放的数据,而蜘蛛网的上蜘蛛比喻成爬虫,而爬虫是可以自动抓取互联网信息的程序,从互联网上抓取一切有价值的信息,并且把站点的html和js返回的图片爬到本地,并且存储方便使用。
- 实践:爬取电影天堂电影详情页
- 网页分析及爬取第一页的详情页url
从电影天堂最新电影界面。可以看到其第一页url为 ygdy8/html/gndy/dyzz/list_23_1.html,第二页为ygdy8/html/gndy/dyzz/list_23_2.html,第三第四页也类似。
from lxml import etree
import requests
url = 'ygdy8/html/gndy/dyzz/list_23_1.html'
headers = {
'User_Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
response = requests.get(url headers=headers)
# response.text 是系统自己默认判断。但很遗憾判断错误,导致乱码出现。我们可以采取另外方式 response.content。自己指定格式解码
# print(response.text)
# print(response.content.decode('gbk'))
print(response.content.decode(encoding="gbk" errors="ignore"))
先以第一页为例,打印数据如下:
分析电影天堂 html 源代码,可以得出每个 table 标签就是一个电影
通过 xpath 拿到每个电影的详情url
html = etree.HTML(text)
detail_urls = html.xpath("//table[@class='tbspan']//a/@href")
for detail_url in detail_urls:
print(detail_url) #加上域名即为详情 url
结果:
2.整理代码并爬取前7页的电影列表url.
from lxml import etree
import requests
# 域名
BASE_DOMAIN = 'ygdy8'
# url = 'ygdy8/html/gndy/dyzz/list_23_1.html'
HEADERS = {
'User_Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
def spider():
base_url = 'ygdy8/html/gndy/dyzz/list_23_{}.html'
for x in range(1 8):
url = base_url.format(x)
print(url) # 求出每一页电影列表的url eg: ygdy8/html/gndy/dyzz/list_23_1.html
if __name__ == '__main__':
spider()
3.爬取每一部电影的详情页地址
def get_detail_urls(url):
response = requests.get(url headers=HEADERS)
# response.text 是系统自己默认判断。但很遗憾判断错误,导致乱码出现。我们可以采取另外方式 response.content。自己指定格式解码
# print(response.text)
# print(response.content.decode('gbk'))
# print(response.content.decode(encoding="gbk" errors="ignore"))
text = response.content.decode(encoding="gbk" errors="ignore")
# 通过 xpath 拿到每个电影的详情url
html = etree.HTML(text)
detail_urls = html.xpath("//table[@class='tbspan']//a/@href")
detail_urls = map(lambda url:BASE_DOMAIN url detail_urls) #这句意思相当于下面一段代码:替换列表中的每一个url
# def abc(url):
# return BASE_DOMAIN url
# index = 1
# for detail_url in detail_urls:
# detail_url = abc(detail_url)
# detail_urls[index] = detail_url
# index 1
return detail_urls
4.抓取电影详情页的数据
# 解析详情页面
def parse_detail_page(url):
movie = {}
response = requests.get(url headers = HEADERS)
text = response.content.decode('gbk' errors='ignore')
html = etree.HTML(text)
# title = html.xpath("//div[@class='title_all']//font[@color='#07519a']") # 本行47行,下面已修改
# 打印出 [<Element font at 0x10cb422c8> <Element font at 0x10cb42308>]
# print(title)
# 为了显示,我们需要转一下编码
# for x in title:
# print(etree.tostring(x encoding='utf-8').decode('utf-8'))
# 我们是为了取得文字,所以修改47行
title = html.xpath("//div[@class='title_all']//font[@color='#07519a']/text()")[0]
movie['title'] = title
zoomE = html.xpath("//div[@id='Zoom']") [0] # 求出共同的顶级容器,方便后面求职
imgs = zoomE.xpath(".//img/@src") # 求出海报和截图
cover = imgs[0]
if len(imgs) > 1:
screenshot = imgs[1]
movie['screenshot'] = screenshot
# print(cover)
movie['cover'] = cover
infos = zoomE.xpath(".//text()")
for index info in enumerate(infos):
if info.startswith('◎年 代'):
info = info.replace("◎年 代" "").strip() # strip 去掉空格
movie['year'] = info
elif info.startswith("◎产 地"):
info = info.replace("◎产 地" "").strip()
movie["country"] = info
elif info.startswith("◎类 别"):
info = info.replace("◎类 别" "").strip()
movie["category"] = info
elif info.startswith("◎豆瓣评分"):
info = info.replace("◎豆瓣评分" "").strip()
movie["douban_rating"] = info
elif info.startswith("◎片 长"):
info = info.replace("◎片 长" "").strip()
movie["duration"] = info
elif info.startswith("◎导 演"):
info = info.replace("◎导 演" "").strip()
movie["director"] = info
elif info.startswith("◎主 演"):
actors = []
actor = info.replace("◎主 演" "").strip()
actors.append(actor)
# 因为主演有很多个,再加上其在电影天堂中元素的特殊性,需要遍历一遍,在分别求出每一个演员
for x in range(index 1 len(infos)): # 从演员 infos 开始遍历,求出每一个演员
actor = infos[x].strip()
if actor.startswith("◎"): # 也就是到了标签 的 ◎ 就退出
break
actors.append(actor)
movie['actor'] = actors
elif info.startswith('◎简 介 '):
# info = info.replace('◎简 介 ' "").strip()
for x in range(index 1 len(infos)):
if infos[x].startswith("◎获奖情况"):
break
profile = infos[x].strip()
movie['profile'] = profile
# print(movie)
elif info.startswith('◎获奖情况 '):
awards = []
# info = info.replace("◎获奖情况 " "").strip()
for x in range(index 1 len(infos)):
if infos[x].startswith("【下载地址】"):
break
award = infos[x].strip()
awards.append(award)
movie['awards'] = awards
# print(awards)
download_url = html.xpath("//td[@bgcolor='#fdfddf']/a/@href")[0]
movie['download_url'] = download_url
return movie
最后结果: