python requests_htmlを利用したスクレイピング AttributeError: '_asyncio.Future' object has no attribute 'html'

実現したいこと

requests-htmlを利用したスクレイピング

発生している問題・エラーメッセージ

TypeError: get() missing 1 required positional argument: 'url'

該当のソースコード

python

1# -*- coding: utf-8 -*-2 3from requests_html import HTMLSession 4 5url = "https://www.google.com/?hl=ja"6 7session = HTMLSession 8r = session.get(url)9r.html.render()

補足情報(FW/ツールのバージョンなど)

Spyder(Python 3.9)で実行
urlは一時的にgoogleのものにしていますが、実際にスクレイピングしたいのは別のサイトです。もともとそのサイトのurlを入れていたのですがエラーが出たためgoogleのurlに変えて試した所、同じエラーが出ました。

質問の更新

melianさんの助言を受けて、
session = HTMLSession

ession = HTMLSession()
に変更しました。
すると、別のエラーが生じました。

実行結果は以下の通りです。公開してもいいのかわからない部分は***に置き換えました。

新たな実行結果

runfile(', wdir='')
Traceback (most recent call last):

File "***", line 356, in compat_exec
exec(code, globals, locals)

File "***", line 9, in <module>
r.html.render()

File "***", line 586, in render
self.browser = self.session.browser # Automatically create a event loop and browser

File "***", line 729, in browser
raise RuntimeError("Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.")

RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.

コードの変更

そこで、さらにコードを2カ所書き変えました。

from requests_html import HTMLSession
->
from requests_html import AsyncHTMLSession

session = HTMLSession()
->
session = AsyncHTMLSession()

全体としては以下の通りです。

該当のソースコード

python

1# -*- coding: utf-8 -*-2 3from requests_html import AsyncHTMLSession 4 5url = "https://www.google.com/?hl=ja"6 7session = AsyncHTMLSession()8r = session.get(url)9r.html.render()

新たな実行結果

runfile('', wdir='')
Traceback (most recent call last):

File "***", line 356, in compat_exec
exec(code, globals, locals)

File "***", line 9, in <module>
r.html.render()

AttributeError: '_asyncio.Future' object has no attribute 'html'

コメントを投稿

0 コメント