teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

質問内容を修正しました。

2020/03/12 00:57

投稿

Justin04
Justin04

スコア11

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

質問内容を変更しました。

2020/03/12 00:57

投稿

Justin04
Justin04

スコア11

title CHANGED
@@ -1,1 +1,1 @@
1
- 【Pythonのスクレイピング】Google検索のプログラムが稼働しません
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.本書ではコマンドプロンプトに「'拡張子なしのファイル名' '検索したいワード'」でログラムが実行されると書いてあるのですが、「'ファイル名.py' '検索したいワード'」にしない「[Errno 2] No such file or directory」エラーてしま
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.BeautifulSoup2引数に'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('Googling...')
19
+ print("Loading...")
18
- res = requests.get('http://google.com/search?q=' + ''.join(sys.argv[1:]))
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('.r a')
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('http://google.com' + link_elems[i].get('href'))
30
+ webbrowser.open("https://google.com" + link_elems[i].get("href"))
29
31
  ```