'str' object has no attribute 'get' 错误解决方案

我在使用python写爬虫时用到了requests.get()方法:

def openUrl(url, ip, agent):
	#函数形参为url:网页地址; ip:ip池; agent:User-Agent, 三者均为字符串类型
	requests.get(url, headers=agent, proxies=ip)

疑惑的是,使用时报了 ‘str’ object has no attribute ‘get’ 错误

查看文档后发现,是由于get()方法中的headers和proxies参数应传入字典而不是字符串,于是经修改,代码成功运行:

def openUrl(url, ip, agent):
	headers = {'User-Agent': agent}
	proxies = {'http' : ip}
	requests.get(url, headers=headers, proxies=proxies)
已标记关键词 清除标记
本人学医,凭兴趣自学python,真心不易,现在遇到一个问题:在写一个简单的注册提交页面,是想通过class将数据存储到本地的txt文件,在输入username和password,submit到register/add时,但遇到了下面的问题: ![图片说明](https://img-ask.csdn.net/upload/201801/27/1517032413_303388.jpg) 请各位大神帮助看是什么问题。 下面是源代码,看看还有没有其他什么问题。谢谢! app.py ``` from flask import ( Flask, render_template, request, redirect, ) from Models import (Users, Requirements) import time import os app = Flask(__name__) @app.route('/', methods=['GET']) def index(): return render_template('/index.html') @app.route('/login') def login(): return render_template('/login.html') @app.route('/register') def register(): return render_template('/register.html') @app.route('/register/add', methods=['POST']) def register_add(): if request.method == 'POST': form = request.form print('打印', form) u = Users.new(form) u.save() return redirect('/login') @app.route('/post', methods=['GET']) def post_message(): return render_template('/post.html') @app.route('/post/add', methods=['POST']) def post_add(): if request.method == 'POST': form = request.form u = Requirements(form) u = Requirements.new(form) u.save() return redirect('/post') if __name__ == '__main__': config = dict( debug=True, host='0.0.0.0', port=2000, ) app.run(**config) ``` Model.py ``` from random import Random import json import os def save(data, path): m = json.dumps(data, ensure_ascii=False) # path = os.path.dirname(__file__) with open(path, 'a+') as f: f.read() f.write(m + '\n') f.close() def load(path): with open(path, 'r', encoding='utf-8') as f: s = f.read() m = json.dumps(s, ensure_ascii=False) print('加载', type(m), m) t = json.loads(m) print('加载', type(t), t) return t def random_str(randomlength=16): s = '' chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789' length = len(chars) - 1 random = Random() for i in range(randomlength): s += chars[random.randint(0, length)] return s class Model(object): @classmethod def db_path(cls): root = os.path.dirname(__file__) classname = cls.__name__ path = '{}/data/{}.txt'.format(root, classname) return path @classmethod def all(cls): """ all 方法(类里面的函数叫方法)使用 load 函数得到所有的 models """ path = cls.db_path() models = load(path) ms = [cls.new(m) for m in models] return ms @classmethod def new(cls, form): m = cls(form) return m def save(self): models = self.all() path = self.db_path() l = [m.__dict__ for m in models] save(l, path) class Users(Model): def __init__(self, form): self.id = random_str() self.username = form.get('username', '') self.password = form.get('password', '') class Requirements(Model): def __init__(self, form): self.id = form.get('id', None) self.province = form.get('province', '') self.city = form.get('city', '') self.post_content = form.get('post_content', '') self.contact_requirement_method = form.get('contact_requirement_method', '') ``` 最后搞定了,采取了下面这种方式,然后有修改了Model部分内容 ``` if request.method == 'POST': formdata = request.form form = formdata.to_dict() u = Users.new(form) u.save() ```
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页