我正在尝试使用Flask编写Web抓取API,这是代码:
app = Flask(__name__)
@app.before_request
def login():
session = requests.Session()
url = "someurl"
params = {'uid': 'username',
'pwd': 'password'}
session.post(url, data=params)
return session
@app.route('/scrape')
def lezione(session):
req = session.get("urlscraped")
soup = BeautifulSoup(req.content, "lxml")
table = soup.find_all(
'table', attrs={'class': 'griglia_tab', 'id': 'data_table'})[2]
if not table:
print("List is empty")
print(re.sub(r'\n\s*\n', '\n', table.text.strip()))
if __name__ == "__main__":
app.run(debug=True, host='localhost', port=5000)
本质上,此代码登录到站点并擦除一些数据。我想要做的是在抓取任何内容之前运行login()
函数,但是当我运行代码时,我得到错误:TypeError: 'Session' object is not callable
代码中有什么问题?
答案 0 :(得分:0)
我建议将login()
转换为常规功能,然后从lezione()
调用它:
def login():
session = requests.Session()
url = "someurl"
params = {'uid': 'username',
'pwd': 'password'}
session.post(url, data=params)
return session
@app.route('/scrape')
def lezione():
req = login().get("urlscraped")
soup = BeautifulSoup(req.content, "lxml")
table = soup.find_all(
'table', attrs={'class': 'griglia_tab', 'id': 'data_table'})[2]
if not table:
print("List is empty")
print(re.sub(r'\n\s*\n', '\n', table.text.strip()))