本课程共五个章节,课程地址:
【Python爬虫教程】花9888买的Python爬虫全套教程2021完整版现分享给大家!(已更新项目)——附赠课程与资料_哔哩哔哩_bilibili
第二章
- 数据解析概述
- 正则表达式
- re模块
- 手刃豆瓣TOP250电影信息
- bs4解析-HTML语法
- bs4解析-bs4模块安装和使用
- 抓取让你睡不着觉的图片
- xpath解析
- 抓取猪八戒数据
目录
(四)手刃豆瓣TOP250电影信息
该节的第二个案例:屠戮盗版天堂电影信息
电影天堂网址:电影天堂_电影下载_高清首发
任务:

点进某部电影的子页面,才能看到该电影的下载链接和名称,如第一行电影点击后跳转到以下页面:2022年美国动作惊悚片《灰影人》HD中英双字迅雷下载_电影天堂
思路:
- 定位到2022必看热片(页面源代码里有) 见第一步和第二步
- 从2022必看热片中提取到子页面的链接地址 见第三步
- 请求子页面的链接地址,拿到我们想要的下载地址和名称 见第四步和第五步
第一步:拿到页面源代码
乱码问题:当前开发环境用的是 utf-8 编码,当我们拿到页面源代码时,默认会把拿到的字节转化成 utf-8。但该网站的编码不是 utf-8,而是 gb2312
import requests
domain = "https://www.dytt89.com/"
resp = requests.get(domain, verify=False) # verify=False 去掉安全验证
resp.encoding = 'gb2312' # 指定字符集
print(resp.text)
第二步:正则匹配
想要的内容(唯一定位:2022必看热片):
import re
obj1 = re.compile(r"2022必看热片.*?<ul>(?P<ul>.*?)</ul>", re.S)
result1 = obj1.finditer(resp.text)
for it in result1:
ul = it.group("ul")
print(ul)
第三步:提取子页面链接
超链接:html中 a标签 表示超链接,如 <a href='url'>周杰伦</a> ,即网页中看到周杰伦三个字,点击这三个字后页面能跳转,跳转至的链接放在 href 里
<li><a href='/i/105992.html' title="2022年美国动作惊悚片《灰影人》HD中英双字">2022年美国动作惊悚片《灰影人》HD中英双字</a><span><font color=#FF0000>07-23</font></span></li>
title 的作用:
鼠标停留在名字上,出来的悬浮的文字
obj2 = re.compile(r"<a href='(?P<href>.*?)'", re.S)
# 提取子页面链接:
result2 = obj2.finditer(ul)
for itt in result2:
print(itt.group("href"))
注意子页面点进去后的链接结构:
故要将子页面链接和 domain(https://www.dytt89.com/) 拼接在一起,才能得到最终的子页面链接。故对以上代码做修改,修改后如下:
obj2 = re.compile(r"<a href='(?P<href>.*?)'", re.S)
child_href_list = []
# 提取子页面链接:
result2 = obj2.finditer(ul)
for itt in result2:
# 拼接子页面的url地址: 域名 + 子页面地址
child_href = domain + itt.group('href').strip("/") # 去掉/
child_href_list.append(child_href) # 把子页面链接保存起来
第四步:拿到子页面源代码
for href in child_href_list:
child_resp = requests.get(href)
child_resp.encoding = 'gb2312'
print(child_resp.text)
break # 测试用
第五步:提取子页面内容
obj3 = re.compile(r'◎片 名(?P<movie>.*?)<br />.*?<td '
r'style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="(?P<download>.*?)">', re.S)
# 提取子页面内容
for href in child_href_list:
child_resp = requests.get(href, verify=False)
child_resp.encoding = 'gb2312'
result3 = obj3.search(child_resp.text)
print(result3.group("movie"))
print(result3.group("download"))
break # 测试用
完整代码:
# 1. 定位到2020必看片
# 2. 从2020必看片中提取到子页面的链接地址
# 3. 请求子页面的链接地址. 拿到我们想要的下载地址....
import requests
import re
domain = "https://www.dytt89.com/"
resp = requests.get(domain, verify=False) # verify=False 去掉安全验证
resp.encoding = 'gb2312' # 指定字符集
# print(resp.text)
# 拿到ul里面的li
obj1 = re.compile(r"2022必看热片.*?<ul>(?P<ul>.*?)</ul>", re.S)
obj2 = re.compile(r"<a href='(?P<href>.*?)'", re.S)
obj3 = re.compile(r'◎片 名(?P<movie>.*?)<br />.*?<td '
r'style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="(?P<download>.*?)">', re.S)
child_href_list = []
result1 = obj1.finditer(resp.text)
for it in result1:
ul = it.group('ul')
# 提取子页面链接:
result2 = obj2.finditer(ul)
for itt in result2:
# 拼接子页面的url地址: 域名 + 子页面地址
child_href = domain + itt.group('href').strip("/") # 去掉/
child_href_list.append(child_href) # 把子页面链接保存起来
# 提取子页面内容
for href in child_href_list:
child_resp = requests.get(href, verify=False)
child_resp.encoding = 'gb2312'
result3 = obj3.search(child_resp.text)
print(result3.group("movie"))
print(result3.group("download"))
# break # 测试用