質問編集履歴
1
完成コード追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,6 +31,57 @@
|
|
31
31
|
div.footer.clearfix > div.footer-item.footer-right >
|
32
32
|
ul.footer-category.service > li > a
|
33
33
|
-->
|
34
|
+
```
|
34
35
|
|
36
|
+
# できました<3
|
37
|
+
[jun68ykt](https://teratail.com/users/jun68ykt)様のアドバイスのおかげで解決しました。`text =` で指定できるとは(≧▽≦) ありがとうございますヽ(`▽´)/
|
35
38
|
|
39
|
+
```python
|
40
|
+
from bs4 import BeautifulSoup
|
41
|
+
import requests
|
42
|
+
import re
|
43
|
+
"""
|
44
|
+
privacy policyまたはプライバシーポリシー、個人情報がサイトにあるか。
|
45
|
+
あればTrueなければFalse
|
46
|
+
"""
|
47
|
+
import warnings
|
48
|
+
warnings.filterwarnings('ignore')
|
49
|
+
|
50
|
+
def get_html():
|
51
|
+
"""
|
52
|
+
- bs4, requestsが必要
|
53
|
+
スクレイピングするならこの関数を最初に使う
|
54
|
+
指定したURLのhtml文書を整列して.htmlに保存。
|
55
|
+
ページからタグを除去して全テキストを抽出。
|
56
|
+
用途: スクレイピングをするときにファイル構造を見る
|
57
|
+
"""
|
58
|
+
r = requests.get("https://www.oreilly.co.jp/index.shtml")
|
59
|
+
r.encoding = r.apparent_encoding
|
60
|
+
html_doc = r.text
|
61
|
+
soup = BeautifulSoup(html_doc)
|
62
|
+
|
63
|
+
html1 = open('Oreilly.html', 'w')
|
64
|
+
html1.write(soup.prettify())
|
65
|
+
html1 = soup.prettify()
|
66
|
+
|
67
|
+
text1 = open('Oreilly.text', 'w')
|
68
|
+
text1.write(soup.get_text())
|
69
|
+
text1 = soup.get_text()
|
70
|
+
|
71
|
+
"""
|
72
|
+
以下からは需要に応じてオプション
|
73
|
+
プライバシーポリシー系を探す
|
74
|
+
Privacy policy、プライバシーポリシー、個人情報
|
75
|
+
"""
|
76
|
+
|
77
|
+
pattern = r"privacy policy|個人情報|プライバシーポリシー"
|
78
|
+
|
79
|
+
with open('Oreilly.html') as f:
|
80
|
+
soup = BeautifulSoup(f.read(), 'lxml')
|
81
|
+
|
82
|
+
for link in soup.findAll('a', text=re.compile(pattern, re.IGNORECASE)):
|
83
|
+
print(link['href'])
|
84
|
+
|
85
|
+
|
86
|
+
get_html()
|
36
87
|
```
|