Python爬虫urllib之猫眼电影正则

猫眼电影

 '''
利用正则爬取猫眼电影
-url:http://maoyan.com/board
-把电影信息拿下来
分析
-一个电影信息在dd的单元内
-找到每一个dd,用re按个查找需要的信息
'''
#1 把网页信息爬取下来
from urllib import request
url = 'http://maoyan.com/board'

rsp = request.urlopen(url)
html = rsp.read().decode()
with open("asp.html", "w",encoding='utf-8')as f:
    f.write(html)
# print(html)

#2 找到每个dd单元
import re

s = r'<dd>(.*?)</dd>'
pattern = re.compile(s, re.S)
films = pattern.findall(html)
print(len(films))

#3 提取出dd单元中需要的信息
for film in films:

    #提取电影名字
    s = r'<a.*?title="(.*?)"'
    pattern = re.compile(s)
    title = pattern.findall(film)[0]
    print(title)

猜你喜欢

转载自blog.csdn.net/qq_31235811/article/details/88770868