用python3爬取豆瓣电影数据

import urllib
from urllib import request
import json
#伪造浏览头
headers = {
    "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Mobile Safari/537.36"
}
#循环遍历每一页
i = 0
while True:
    url = "https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=%d"%(i*20)
    i += 1
    # print(url)
    #访问网页
    req = urllib.request.Request(url,headers = headers)
    response = urllib.request.urlopen(req)
    # print(response)
    #做请求判断,如果为返回200,可爬取
    if response.code == 200:
        movedata = response.read().decode('utf-8')
        print(movedata)

    #     电影数据
        data = json.loads(movedata)['data']
        for movie in data:
            #获取导演
            directors = movie['directors']
            # 获取电影名
            title = movie['title']
            # 获取主要演员
            casts = movie['casts']
            # 获取豆瓣评分
            rate = movie['rate']

            print(directors,title,casts)

    else:
        break

你可能感兴趣的:(python笔记,爬虫)