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

回答編集履歴

7

テキスト修正

2018/10/23 22:53

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -57,6 +57,6 @@
57
57
 
58
58
  参考までに、以下は私の手元のMacで実行したログです。
59
59
 
60
- ![イメージ説明](9ec42e26b7f36d47a34e57adad76a42b.png)
60
+ ![イメージ説明](0a4e49e5c14915e6d00ec7617b5920ad.png)
61
61
 
62
62
  以上、参考になれば幸いです。

6

テキスト修正

2018/10/23 22:53

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -14,10 +14,12 @@
14
14
  items = [item[search_attr] for item in soup.find_all() if search_attr in item.attrs]
15
15
  print(items)
16
16
  ```
17
+  
18
+   
19
+ 上記の scraping.py を使って、以下の test.html を解析します。
20
+ (※ scraping.py と test.html は、 同じディレクトリ内に作成してあるものとします)
17
21
 
18
- 上記の scraping.py を使って、以下の test.html を解析します。 (※ scraping.py と test.html は、 同じディレクトリ内に作成してあるものとします)
19
22
 
20
-
21
23
  #### **test.html** (テスト用HTML):
22
24
  ```html
23
25
  <!DOCTYPE html>

5

テキスト修正

2018/10/23 22:22

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,7 +1,23 @@
1
1
  こんにちは。
2
2
 
3
- 以下のような方法でいかがでしょうか?
3
+ 以下のようなスクリプトでいかがでしょうか?
4
4
 
5
+ #### **scraping.py**:
6
+
7
+ ```python
8
+ from bs4 import BeautifulSoup
9
+
10
+ search_attr = 'data-tooltip-content'
11
+
12
+ with open('./test.html') as f:
13
+ soup = BeautifulSoup(f.read(), 'lxml')
14
+ items = [item[search_attr] for item in soup.find_all() if search_attr in item.attrs]
15
+ print(items)
16
+ ```
17
+
18
+ 上記の scraping.py を使って、以下の test.html を解析します。 (※ scraping.py と test.html は、 同じディレクトリ内に作成してあるものとします)
19
+
20
+
5
21
  #### **test.html** (テスト用HTML):
6
22
  ```html
7
23
  <!DOCTYPE html>
@@ -33,27 +49,12 @@
33
49
  ```
34
50
 
35
51
 
36
- #### **scraping.py**:
52
+ **scraping.py** を実行すると以下が表示されると思います。
37
53
 
38
- 上記の test.html を、以下の scraping.py で解析します。 (※ scraping.py と test.html は、 同じディレクトリ内に作成してあるものとします)
39
-
40
- ```python
41
- from bs4 import BeautifulSoup
42
-
43
- search_attr = 'data-tooltip-content'
44
-
45
- with open('./test.html') as f:
46
- soup = BeautifulSoup(f.read(), 'lxml')
47
- items = [item[search_attr] for item in soup.find_all() if search_attr in item.attrs]
48
- print(items)
49
- ```
50
-
51
-
52
-
53
- 上記の **scraping.py** を実行すると以下が表示されると思います。
54
-
55
54
  > ['缺貨中1', '缺貨中2', '缺貨中3']
56
55
 
57
56
  参考までに、以下は私の手元のMacで実行したログです。
58
57
 
59
- ![イメージ説明](9ec42e26b7f36d47a34e57adad76a42b.png)
58
+ ![イメージ説明](9ec42e26b7f36d47a34e57adad76a42b.png)
59
+
60
+ 以上、参考になれば幸いです。

4

テキスト修正

2018/10/23 22:21

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -54,4 +54,6 @@
54
54
 
55
55
  > ['缺貨中1', '缺貨中2', '缺貨中3']
56
56
 
57
- 以上、参考になれば幸いです。
57
+ 参考まで、以下は私の手元のMac実行したログです。
58
+
59
+ ![イメージ説明](9ec42e26b7f36d47a34e57adad76a42b.png)

3

テキスト修正

2018/10/23 22:09

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
 
52
52
 
53
- 上記の **scraping.py** を実行して、うまくいけば以下が表示されると思います。
53
+ 上記の **scraping.py** を実行すると以下が表示されると思います。
54
54
 
55
55
  > ['缺貨中1', '缺貨中2', '缺貨中3']
56
56
 

2

テキスト修正

2018/10/23 21:59

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -32,11 +32,11 @@
32
32
  </html>
33
33
  ```
34
34
 
35
- 上記の test.html を、以下の scraping.py で解析します。 (※ test.html と scraping.py は同じディレクトリ内に作成してあるものとします)
35
+
36
-   
37
-  
38
36
  #### **scraping.py**:
39
37
 
38
+ 上記の test.html を、以下の scraping.py で解析します。 (※ scraping.py と test.html は、 同じディレクトリ内に作成してあるものとします)
39
+
40
40
  ```python
41
41
  from bs4 import BeautifulSoup
42
42
 

1

テキスト修正

2018/10/23 21:57

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  以下のような方法でいかがでしょうか?
4
4
 
5
- - **test.html** (テスト用HTML):
5
+ #### **test.html** (テスト用HTML):
6
6
  ```html
7
7
  <!DOCTYPE html>
8
8
  <html lang="ja">
@@ -32,10 +32,11 @@
32
32
  </html>
33
33
  ```
34
34
 
35
- 上記の test.html を、以下の scraping.py で解析し、`data-tooltip-content` 属性を持つ要素について、この属性の値をリストにします。(※ test.html と scraping.py は同じディレクトリ内に作成してあるものとします)
35
+ 上記の test.html を、以下の scraping.py で解析します。 (※ test.html と scraping.py は同じディレクトリ内に作成してあるものとします)
36
+   
37
+  
38
+ #### **scraping.py**:
36
39
 
37
- - **scraping.py**:
38
-
39
40
  ```python
40
41
  from bs4 import BeautifulSoup
41
42
 
@@ -47,6 +48,8 @@
47
48
  print(items)
48
49
  ```
49
50
 
51
+
52
+
50
53
  上記の **scraping.py** を実行して、うまくいけば以下が表示されると思います。
51
54
 
52
55
  > ['缺貨中1', '缺貨中2', '缺貨中3']