python爬虫之豆瓣网及作业

1作业

此作业至今未运行出结果,有待验证

# 找规律
# 基本思路获取每个页面的url,通过params函数可获得
# <a href="?start=0&amp;filter=">1</a>
# <a href="?start=25&amp;filter=">2</a>
import requests
import  re
url='https://movie.douban.com/top250'
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'
}
page_number=0
while page_number<226:
    response=requests.get(url,headers=headers,params={'start':'page_number'})
    movie_content_list=re.findall('<div class="item">.*?href="(.*?)">.*?src="(.*?)">.*?<span class="title">(.*?)</span>.*? <p class="">导演:(.*?).*?主演:(.*?).*?...(.*?)/.*?/(.*?)</p>.*?<span class="rating.num".*?>(.*?)</span>.*?<span>(.*?)人评价',response.text,re.S)
    for movie_content in movie_content_list:
        detail_url,movie_jpg,name,director,leading_role,year,introduce,point,num=movie_content
        data=f'电影名称:{name},详情页:{detail_url},图片url:{movie_jpg},导演:{director},主演:{leading_role},上映时间:{year},简介:{introduce},评分:{point},评价人数:{num}\n'
        print(data)
    with open('豆瓣的top250电影.txt','a',encoding='utf-8')as f:
        f.write(data)
    page_number=page_number+25

 

2.豆瓣电影排行榜TOP25

'''
主页:
    https://movie.douban.com/top250
    GET
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36
    re正则:
    如何书写正则表达式,根据.*?一直匹配到自己想要查找的内容,中间的内容就过滤
    在此需要找到固定标识,再通过(.*?)提取到自己想要的内容
    <div class="item">.*?href="(.*?)">.*?src="(.*?)">.*?<span class="title">(.*?)</span>.*?<span class="rating.num".*?>(.*?)</span>.*?<span>(.*?)人评价
    <div class="item">.*?href="(.*?)">.*?src="(.*?)">.*?<span class="title">(.*?)</span>.*? <p class="">导演:(.*?).*?主演:(.*?).*?...(.*?)/.*?/(.*?)</p>.*?<span class="rating.num".*?>(.*?)</span>.*?<span>(.*?)人评价
        <div class="item">
                <div class="pic">
                    <em class="">2</em>
                    <a href="https://movie.douban.com/subject/1291546/">
                        <img width="100" alt="霸王别姬" src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1910813120.webp" class="">
                    </a>
                </div>
                <div class="info">
                    <div class="hd">
                        <a href="https://movie.douban.com/subject/1291546/" class="">
                            <span class="title">霸王别姬</span>
                                <span class="other">&nbsp;/&nbsp;再见,我的妾  /  Farewell My Concubine</span>
                        </a>


                            <span class="playable">[可播放]</span>
                    </div>
                    <div class="bd">
                        <p class="">
                            导演: 陈凯歌 Kaige Chen&nbsp;&nbsp;&nbsp;主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
                            1993&nbsp;/&nbsp;* 香港&nbsp;/&nbsp;剧情 爱情 同性
                        </p>


                        <div class="star">
                                <span class="rating5-t"></span>
                                <span class="rating_num" property="v:average">9.6</span>
                                <span property="v:best" content="10.0"></span>
                                <span>1074955人评价</span>
                        </div>

                            <p class="quote">
                                <span class="inq">风华绝代。</span>
                            </p>
                    </div>
                </div>
            </div>
'''

import requests
import  re
url='https://movie.douban.com/top250'
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'
}
# 1.向豆瓣top250发送请求获取响应数据
response=requests.get(url,headers=headers)
# 2.通过正则提取数据
# 需要提取的数据为:电影详情页url,图片链接,电影名称,电影评分,评价人数
movie_content_list=re.findall(' <div class="item">.*?href="(.*?)">.*?src="(.*?)">.*?<span class="title">(.*?)</span>.*?<span class="rating.num".*?>(.*?)</span>.*?<span>(.*?)人评价',response.text,re.S)
# 解析文本 response.text
# 匹配模式 re.S
# 4.使用元组解压赋值每一部电影
for movie_content in movie_content_list:
    detail_url,movie_jpg,name,point,num=movie_content  # 逐个取出列表中获取的电影数据,因为取出的是元组模式,所以将其中的内容对应赋值给每一个变量 detail_url,movie_jpg,name,num=movie_content
    data=f'电影名称:{name},详情页:{detail_url},图片url:{movie_jpg},评分:{point},评价人数:{num}\n'
    print(data)
# 5.保存数据,把电影信息写入文件中
with open('爬取豆瓣的前25部电影.txt','a',encoding='utf-8')as f:
    f.write(data)
# 找到每一页的规律
# 爬取豆瓣的250部电影
# 提取画框信息

 

上一篇:Zookeeper知识点梳理


下一篇:谈谈Zookeeper的ZAB协议