我正在尝试为自己创建一个脚本,以便在一些分类网站上使用并从cl开始,我使用的是烧瓶网框架和robobrowser但不是很顺利。
目标 它将采用我的预设值并将其放入该分类广告网站的字段中。看起来似乎是一个困难的概念但是在网上阅读了5个小时的不同代码和反复试验之后,我记得最好的开发人员正在筹码......
我应该告诉你我是Python新手并且还有很多东西要学习,所以任何帮助都会非常感激。
我得到的错误是: 断言isinstance(形式,'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()
答案 0 :(得分:0)
isinstance返回true,否则返回false。在断言中,表单变量的类型为robobrowser.forms.form.Form。您可以使用以下代码查看此内容:
print(type(form)) # should print <class 'robobrowser.forms.form.Form'>
如果您更新第二个参数以指示此robobrowser Form类,则您的特定断言将通过:
# Add the following to your imports
import robobrowser
# ... existing code below imports
# The following should be true if form is valid
assert isinstance(form, robobrowser.forms.form.Form)
您也可以直接导入Form类,但是您必须相应地更新断言:
from robobrowser.forms.form import Form
# ... existing code below imports
assert isinstance(form, Form)
修改强>
以下是从该页面正确获取表单的代码。没有任何ID为“登录”的表单。但您可以通过使用其操作或抓取页面上的第一个表单来选择它。
# ... previous code ...
form = browser.get_form(action='https://accounts.craigslist.org/login')
# you can also use: form = browser.get_form(0)
# It looks like you aren't selecting the fields by their proper names either.
form['inputEmailHandle'] = 'fill in username here'
form['inputPassword'] = 'fill in password here'
browser.submit_form(form)
查看上面添加的代码是否有帮助。