Python学习的第六天

豆瓣网爬虫——json

  • 代码
import requests
import json

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}

url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=0'

response = requests.get(url, headers=headers)
print(response.status_code)

#  获取响应  json 字符串
data = response.content.decode()
# print(data)
# print(type(data))

# 把json 字符串转化成  python类型
dict_data = json.loads(data)
# print(type(dict_data))
# print(dict_data)

# 提取数据
tv_list = dict_data['subjects']
# print(tv_list)

data_list = []
# 遍历
for tv in tv_list:
    temp = {}
    # 电视剧名
    temp['title'] = tv['title']
    # rate
    temp['rate'] = tv['rate']
    #  封面
    temp['cover'] = tv['cover']
    # url
    temp['url'] = tv['url']
    # [{},{},{}]
    data_list.append(temp)

for tv in  data_list:
    print(tv)

# 存储成json

f = open('douban.json','w', encoding='utf-8')
# 写入  ensure_ascii指定false
json.dump(data_list, f, ensure_ascii=False)
  • 复制网址


    image.png
  • 运行截图


    image.png
  • 写入json文件


    image.png

你可能感兴趣的:(Python学习的第六天)