juzi_go 2021-11-14 14:52 采纳率: 80%
浏览 72
已结题

python爬虫然后把数据保存到csv中 但是爬不到内容

python 爬豆瓣电视剧然后保存到csv中 但是爬不到内容 下面的代码怎么修改

import requests
import csv


r_header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'}
base_url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=50&page_start=0'
r_paras = {'type': 'tv',
           'tag': '最新',
           'sort': 'recommend',
           'page_limit': 50,
           'page_start': 0}
rest = requests.get(base_url, headers=r_header, params=r_paras)

movie_dict = rest.json()
print(movie_dict['subjects'])
# movies = []
# for item in movie_dict['subjects']:
#     movies.append([item['title'], item['url']])
# print(movies)


# with open('douban_movies.csv', 'a+', encoding='utf-8', newline='') as f:
#     w = csv.writer(f)
#     w.writerows(movies)

json数据

{
    "subjects":[
        {
            "episodes_info":"更新至5集",
            "rate":"9.6",
            "cover_x":1600,
            "title":"国王排名",
            "url":"https://movie.douban.com/subject/34927946/",
            "playable":true,
            "cover":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2681362557.webp",
            "id":"34927946",
            "cover_y":2262,
            "is_new":false
        },
        {
            "episodes_info":"更新至12集",
            "rate":"",
            "cover_x":1800,
            "title":"斛珠夫人",
            "url":"https://movie.douban.com/subject/26798457/",
            "playable":true,
            "cover":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2714598490.webp",
            "id":"26798457",
            "cover_y":3200,
            "is_new":true
        },
        {
            "episodes_info":"",
            "rate":"6.8",
            "cover_x":1024,
            "title":"现在正在分手中",
            "url":"https://movie.douban.com/subject/35131286/",
            "playable":false,
            "cover":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2709863550.webp",
            "id":"35131286",
            "cover_y":1464,
            "is_new":true
        },
    ]
}

  • 写回答

1条回答 默认 最新

  • 坚持不懈的大白 前端领域优质创作者 2021-11-15 10:19
    关注

    这样就可以了

    import requests
    import csv
    
    r_header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'}
    base_url = 'https://movie.douban.com/j/search_subjects'
    r_paras = {'type': 'tv',
               'tag': '热门',
               'sort': 'recommend',
               'page_limit': 50,
               'page_start': 0}
    rest = requests.get(base_url, headers=r_header, params=r_paras)
    print(rest.text)
    movie_dict = rest.json()
    print(movie_dict['subjects'])
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月23日
  • 已采纳回答 11月15日
  • 创建了问题 11月14日

悬赏问题

  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集
  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥50 MacOS 使用虚拟机安装k8s