質問編集履歴

4

内容の訂正 (旧)を追加

2023/01/27 10:25

投稿

Takananashi
Takananashi

スコア3

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  requests-htmlを利用したスクレイピング
4
4
 
5
- ### 発生している問題・エラーメッセージ
5
+ ### (旧)発生している問題・エラーメッセージ
6
6
 
7
7
  ```
8
8
  TypeError: get() missing 1 required positional argument: 'url'
9
9
  ```
10
10
 
11
- ### 該当のソースコード
11
+ ### (旧)該当のソースコード
12
12
 
13
13
  ```python
14
14
  # -*- coding: utf-8 -*-

3

あらたなエラー

2023/01/27 10:06

投稿

Takananashi
Takananashi

スコア3

test CHANGED
File without changes
test CHANGED
@@ -97,3 +97,8 @@
97
97
  r.html.render()
98
98
 
99
99
  AttributeError: '_asyncio.Future' object has no attribute 'html'
100
+
101
+ ### 質問
102
+
103
+ このエラーはどう解決すればいいでしょうか。
104
+ また、AsyncHTMLSessionに変えない方がよかったのでしょうか。

2

発生した新たなエラー

2023/01/27 09:59

投稿

Takananashi
Takananashi

スコア3

test CHANGED
@@ -1 +1 @@
1
- python requests_htmlを利用したスクレイピング TypeError: get() missing 1 required positional argument: 'url'
1
+ python requests_htmlを利用したスクレイピング AttributeError: '_asyncio.Future' object has no attribute 'html'
test CHANGED
@@ -57,6 +57,8 @@
57
57
 
58
58
  RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.
59
59
 
60
+ ### コードの変更
61
+
60
62
  そこで、さらにコードを2カ所書き変えました。
61
63
 
62
64
  from requests_html import HTMLSession

1

最初に質問した時とは別のエラーが生じた。

2023/01/27 09:58

投稿

Takananashi
Takananashi

スコア3

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,71 @@
27
27
  Spyder(Python 3.9)で実行
28
28
  urlは一時的にgoogleのものにしていますが、実際にスクレイピングしたいのは別のサイトです。もともとそのサイトのurlを入れていたのですがエラーが出たためgoogleのurlに変えて試した所、同じエラーが出ました。
29
29
 
30
+ ### 質問の更新
31
+
32
+ melianさんの助言を受けて、
33
+ session = HTMLSession
34
+
35
+ ession = HTMLSession()
36
+ に変更しました。
37
+ すると、別のエラーが生じました。
38
+
39
+ 実行結果は以下の通りです。公開してもいいのかわからない部分は***に置き換えました。
40
+
41
+ ### 新たな実行結果
42
+
43
+ runfile('***, wdir='***')
44
+ Traceback (most recent call last):
45
+
46
+ File "***", line 356, in compat_exec
47
+ exec(code, globals, locals)
48
+
49
+ File "***", line 9, in <module>
50
+ r.html.render()
51
+
52
+ File "***", line 586, in render
53
+ self.browser = self.session.browser # Automatically create a event loop and browser
54
+
55
+ File "***", line 729, in browser
56
+ raise RuntimeError("Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.")
57
+
58
+ RuntimeError: Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead.
59
+
60
+ そこで、さらにコードを2カ所書き変えました。
61
+
62
+ from requests_html import HTMLSession
63
+ ->
64
+ from requests_html import AsyncHTMLSession
65
+
66
+ session = HTMLSession()
67
+ ->
68
+ session = AsyncHTMLSession()
69
+
70
+ 全体としては以下の通りです。
71
+
72
+ ### 該当のソースコード
73
+
74
+ ```python
75
+ # -*- coding: utf-8 -*-
76
+
77
+ from requests_html import AsyncHTMLSession
78
+
79
+ url = "https://www.google.com/?hl=ja"
80
+
81
+ session = AsyncHTMLSession()
82
+ r = session.get(url)
83
+ r.html.render()
84
+ ```
85
+
86
+ ### 新たな実行結果
87
+
88
+ runfile('***', wdir='***')
89
+ Traceback (most recent call last):
90
+
91
+ File "***", line 356, in compat_exec
92
+ exec(code, globals, locals)
93
+
94
+ File "***", line 9, in <module>
95
+ r.html.render()
96
+
97
+ AttributeError: '_asyncio.Future' object has no attribute 'html'