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

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条回答 默认 最新

  • il_持之以恒_li Python领域新星创作者 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'])
    
    
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题