python编程数据爬取3

2、破解百度翻译

import requests
import json
if __name__ == '__main__':
    #UA伪装:让爬虫对应的请求载体身份标识伪装成某一款浏览器
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.0.10191 SLBChan/103'
    }
    post_url='https://fanyi.baidu.com/sug'
    kw=input('输入字段')
    data = {
        'kw':kw
    }
    response=requests.post(url=post_url,data=data,headers=headers)
    dic_obj =response.json()
    fileName =kw+'.json'
    fp=open(fileName,'w',encoding='utf-8')
    json.dump(dic_obj,fp=fp,ensure_ascii=False)
    print('爬取数据完成')

爬取结果

 

3、豆瓣电影排行

import requests
import json
if __name__ == '__main__':
  #网页检查
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.0.10191 SLBChan/103'
    }
    url=' https://movie.douban.com/j/chart/top_list'
#运行界面时,进入检查界面中的XHR,找到json数据
param = { 'type':'24', 'interval_id':'100:90', 'action':'', 'start':'0',#从库的第几部电影去取 'limit':'20',#一次取出的个数 } response = requests.get(url=url, params=param, headers=headers) list_data =response.json() fp=open('./douban.json','w',encoding='utf-8') json.dump(list_data,fp=fp,ensure_ascii=False) print('爬取数据完成')

爬取结果

 

json在线转化

 

posted @ 2024-01-13 22:14  伽澄  阅读(7)  评论(0编辑  收藏  举报