当前位置:   article > 正文

Python爬虫爬取豆瓣top250

Python爬虫爬取豆瓣top250

安装相应库以后,直接复制代码,运行,输入起始页码 1,截至页码 10,就可以了,

豆瓣是一页25条数据(现在是2022年5月1日,以后人家更新具体会换每页几条数据不确定,但是随之页码也要换,不换就有可能报错)

  1. import urllib.request
  2. import urllib.parse
  3. def create_request(page):
  4. base_url = "https://movie.douban.com/top250?"
  5. data={
  6. 'start':(page-1)*25,
  7. 'filter':20
  8. }
  9. data= urllib.parse.urlencode(data)
  10. url = base_url + data
  11. print(url)
  12. headers={
  13. # 'User - Agent': 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 99.0.4844.51Safari / 537.36Edg / 99.0.1150.39'
  14. 'Cookie': 'll="118161"; bid=5cBVpNqid80; _vwo_uuid_v2=D45E4E4B1EAE7035F4686A86B9A0C0134|17ac109a1ce3a940cd607eea69989c60; __utmc=30149280; __utmc=223695111; ap_v=0,6.0; __gads=ID=032c05146012f52c-22deaa68eed10084:T=1649916778:RT=1649916778: S=ALNI_MZ6cKzkpPoKMyiGFtLc6oVMSvkQww; __utma=30149280.638397394.1649408408.1649916715.1649920526.3; __utmb=30149280.0.10.1649920526; __utmz=30149280.1649920526.3.3.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utma=223695111.1472431755.1649408412.1649916715.1649920526.3; __utmb=223695111.0.10.1649920526; __utmz=223695111.1649920526.3.3.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1649920526%2C%22https%3A%2F%2Fcn.bing.com%2F%22%5D; _pk_id.100001.4cf6=826d7eb61914769a.1649408412.3.1649920526.1649917562.; _pk_ses.100001.4cf6=*',
  15. 'DNT': ' 1',
  16. 'Host': ' movie.douban.com',
  17. 'Referer':' https://cn.bing.com/',
  18. 'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36',
  19. }
  20. request=urllib.request.Request(url=url,headers=headers)
  21. return request
  22. def get_content(request):
  23. response=urllib.request.urlopen(request)
  24. content=response.read().decode('utf-8')
  25. return content
  26. def download(page,content):
  27. with open('./豆瓣top250/douban_'+str(page)+'.json','w',encoding='utf-8')as fp: # 你需要在同文件夹下创建名字为 豆瓣top250的文件夹 不然会报错找不到文件夹
  28. fp.write(content)
  29. if __name__ == '__main__':
  30. start_page = int(input('请输入开始:'))
  31. end_page = int(input('请输入结束:'))
  32. for page in range(start_page,end_page+1):
  33. request=create_request(page)
  34. content=get_content(request)
  35. download(page,content)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/488144
推荐阅读
相关标签
  

闽ICP备14008679号