質問編集履歴
2
質問内容を修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
→2.に関してはbs4.BeautifulSoupの第2引数に'html.parser'を入れて解決しましたが、コマンドプロンプトで実行すると「Loading...」が表示されるだけ、Anacondaのインタラクティブシェルで実行するとAttributeError: '_ModuleLock' object has no attribute 'name'が表示されてしまいます。
|
10
10
|
|
11
|
-
_Modulelockを調べたのですがさっぱり分からず…。申し訳ありませんが教えて下さ。よろしくお願い致します。
|
11
|
+
_Modulelockを調べたのですがさっぱり分からず…。申し訳ありませんが教えて下さい。よろしくお願い致します。
|
12
12
|
|
13
13
|
```python
|
14
14
|
#! python3
|
1
質問内容を変更しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Python
|
1
|
+
【Python】コマンドプロンプトからのPython起動とスクレイピング
|
body
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
「退屈なことはPythonにやらせよう」11章のWebスクレイピングで、Googleの検索結果上位5件を自動で開くプログラムを書いているのですが、
|
1
|
+
「退屈なことはPythonにやらせよう」11章のWebスクレイピングで、Googleの検索結果上位5件を自動で開くプログラムを書いているのですが、下記の2点で躓いています。
|
2
2
|
|
3
|
-
1.本書ではコマンドプロンプトに「'拡張子なしのファイル名' '検索したいワード'」で
|
3
|
+
1.本書ではコマンドプロンプトに「'拡張子なしのファイル名' '検索したいワード'」でプログラムが実行されると書いてあるのですが、「[Errno 2] No such file or directory」のエラーが出てしまう。「'ファイル名.py' '検索したいワード'」にすると上手くいくのですが、違いが調べてもわかりません。PathにPython.exeのディレクトリを設定したりスクリプトがあるフォルダを設定しましたが駄目でした。
|
4
4
|
|
5
5
|
2.実行すると、「C:\Users\ユーザ名\Documents\Python Scripts\lucky.py:11: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
|
6
6
|
|
7
7
|
The code that caused this warning is on line 11 of the file C:\Users\s2160085.MAX-D4.000\Documents\Python Scripts\lucky.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.」という表記が出て検索結果が表示されない。
|
8
8
|
|
9
|
-
の2
|
9
|
+
→2.に関してはbs4.BeautifulSoupの第2引数に'html.parser'を入れて解決しましたが、コマンドプロンプトで実行すると「Loading...」が表示されるだけ、Anacondaのインタラクティブシェルで実行するとAttributeError: '_ModuleLock' object has no attribute 'name'が表示されてしまいます。
|
10
10
|
|
11
|
+
_Modulelockを調べたのですがさっぱり分からず…。申し訳ありませんが教えて下さ。よろしくお願い致します。
|
12
|
+
|
11
13
|
```python
|
12
14
|
#! python3
|
13
15
|
# lucky.py
|
14
16
|
|
15
17
|
import requests, sys, webbrowser, bs4
|
16
18
|
|
17
|
-
print(
|
19
|
+
print("Loading...")
|
18
|
-
res = requests.get(
|
20
|
+
res = requests.get("https://google.com/search?q=" + " ".join(sys.argv[1:]))
|
19
21
|
res.raise_for_status()
|
20
22
|
|
21
23
|
#TODO 上位の検索結果のリンクを取得する
|
22
|
-
soup = bs4.BeautifulSoup(res.text)
|
24
|
+
soup = bs4.BeautifulSoup(res.text, "html.parser")
|
23
|
-
link_elems = soup.select(
|
25
|
+
link_elems = soup.select(".r a")
|
24
26
|
|
25
27
|
#TODO 各結果をブラウザのタブで開く
|
26
28
|
num_open = min(5, len(link_elems))
|
27
29
|
for i in range(num_open):
|
28
|
-
webbrowser.open(
|
30
|
+
webbrowser.open("https://google.com" + link_elems[i].get("href"))
|
29
31
|
```
|