【发布时间】:2015-07-16 04:49:35
【问题描述】:
我正在尝试为自己创建一个脚本,以便在一些分类网站上使用,并从 cl 开始,我正在使用 flask web 框架和 robobrowser,但进展并不顺利。
目标它将采用我的预设值并将它们放入该分类网站的字段中。这似乎不是一个困难的概念,但是在阅读了 5 个小时的在线不同代码和反复试验后,我记得最好的开发人员都在堆栈中......
我应该告诉您,我是 Python 新手,还有很多东西要学习,因此我们将不胜感激。
我得到的错误是: 断言 isinstance(form, 'str') TypeError: isinstance() arg 2 必须是类型或类型的元组
但我不知道如何解决这个问题并且完全迷失了。帮助!!! 提前致谢
# autosubmit to site
from flask import Flask
from robobrowser import RoboBrowser
app = Flask(__name__)
@app.route('/', methods = ['GET', 'POST'])
class My_RoboBrowser(RoboBrowser):
def __init__(self, auth=None, parser=None, headers=None, user_agent=None, history=True):
RoboBrowser.__init__(self, parser=None, user_agent=None, history=True)
def Open(self, vURL, vVerify=True):
response = self.session.get(vURL, verify=vVerify)
self._update_state(response)
browser = My_RoboBrowser(RoboBrowser, "html.parser");
urlL = 'https://accounts.craigslist.org/login'
browser.Open(urlL)
form = browser.get_form(id='login')
assert isinstance(form, 'str')
form['username'] = 'username'
form['password'] = 'password'
browser.submit_form(form)
urlQ = 'https://post.craigslist.org/k/qGDv7K4q5RGD0B5ZEBgXOQ/GLzgd?s=edit'
browser.open(urlQ)
#Question_Tag = browser.find_all(class_="not_answered")[0]
#ID = Question_Tag.get('data-qid')
#Get the form to fill out
Form = browser.get_form(id='formpost')
Form['PostingTitle'].value = 'Create this advertise ment in py'
Form['Postal_code'].value = ['10543']
Form['PostingBody'].value = 'TOGETHER WE INNOVATE Stress free communication with developers that are in the United States. We pride ourselves in bringing your web and app ideas to life and keeping your data secured'
browser.submit_form(Form)
if __name__ == "__main__":
app.run()
【问题讨论】:
-
isinstance(form, str)看到了吗?str不是'str' -
@mehtunguh 我删除了这些引号,但仍然收到错误 'assert isinstance(form, str) AssertionError '
-
AssertionError在这种情况下意味着对象不是str的实例。您的代码是正确的,但您的表单不是字符串。看起来可能是dict。
标签: python forms beautifulsoup robobrowser