我正在嘗試使用 requests_html 在 PyCharm 中使用 python 進行網路抓取。以下代碼在使用與我的 PyCharm 專案(python 3.8.10)相同的 venv 的終端中不會回傳錯誤,但是當我使用 PyCharm 時出現錯誤。
代碼:
from requests_html import HTMLSession
session = HTMLSession
response = session.get('https://python.org')
response = session.get('https://python.org')在 PyCharm 中運行后,我收到此錯誤:
Traceback (most recent call last):
File "/usr/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 11, in <module>
TypeError: get() missing 1 required positional argument: 'url'
如何使 PyCharm 成功運行session.get()?
uj5u.com熱心網友回復:
看起來您在這里不小心遺漏了括號:
session = HTMLSession
所以:session.get--> <function Session.get at 0x10377e550>。
help(session.get)--> get(self, url, **kwargs)。
它試圖呼叫get()類 HTMLSession 上的方法,而不是類的實體,即會話。在這種情況下,函式接收字串引數代替隱式self引數,并且它不接收url引數。
而之后:
session = HTMLSession()
session.get--><bound method Session.get of <requests_html.HTMLSession object at 0x10276c460>>
help(session.get)--> get(url, **kwargs)。
session.get('https://python.org')--> <Response [200]>。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/439582.html
上一篇:迭代地將新串列作為值添加到字典中
