Skip to content

Commit 701c508

Browse files
authored
Merge pull request #10 from jimmy15923/patch-11
Revise session A&B
2 parents 64a8e66 + 807a023 commit 701c508

25 files changed

+448
-269
lines changed

Session_A/answer/02_regular_expression_answer.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@
479479
"source": [
480480
"# 如果忘記怎麼寫 requests 或 BeautifulSoup,可以參考\n",
481481
"\n",
482-
"# response = requests.get(\"http://yp.518.com.tw/service-life.html?ctf=10\")\n",
482+
"# response = requests.get(\"https://jimmy15923.github.io/518\")\n",
483483
"# print(response.encoding)\n",
484484
"\n",
485485
"# soup = BeautifulSoup(response.text, \"lxml\")"
@@ -494,7 +494,7 @@
494494
"name": "stdout",
495495
"output_type": "stream",
496496
"text": [
497-
"UTF-8\n",
497+
"utf-8\n",
498498
"['02-29242789', '03-4709933', '04-23601719', '06-2092929', '05-2238686', '07-6994433', '07-3610768', '02-29662939', '02-29662939', '02-29609370']\n"
499499
]
500500
}
@@ -505,7 +505,8 @@
505505
"from bs4 import BeautifulSoup\n",
506506
"import re\n",
507507
"\n",
508-
"response = requests.get(\"http://yp.518.com.tw/service-life.html?ctf=10\") # requests 518 網頁 並拿到 response\n",
508+
"## 518 網頁伺服器無法容納多人同時 requests,請大家使用以下的網頁作 requests,其 html 的內容是一模一樣的\n",
509+
"response = requests.get(\"https://jimmy15923.github.io/518\") # requests 518 網頁 並拿到 response\n",
509510
"print(response.encoding) # 印出 encoding 結果\n",
510511
"soup = BeautifulSoup(response.text, \"lxml\") # 將 HTML 丟給 BeautifulSoup 作解析\n",
511512
"\n",

Session_A/answer/02_regular_expression_answer.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
# ### 範例 02-1: *, +, {} 的用法
2626
# \* 代表前面的字元可出現零次以上,而 + 則是代表前面的字元至少要出現一次以上,{m,n} 則是代表前面的字元可出現 m 次 ~ n 次
2727

28-
# In[24]:
28+
# In[1]:
29+
30+
import re
2931

3032
pattern = "a+b*c"
3133
test_string = 'find aabc, ac, skip abb, dd'
@@ -37,7 +39,9 @@
3739
#
3840
# Hint: 思考一下要尋找的文字跟要濾除的文字,在字母之間有甚麼差異,先把 find 寫出來,再想辦法去掉要 skip
3941

40-
# In[20]:
42+
# In[2]:
43+
44+
import re
4145

4246
### your codes
4347
pattern = "a*b+c"
@@ -48,7 +52,9 @@
4852
# ### 範例 02-2: 找到英數字
4953
# 中括號代表的意思是「這個字元可以是括號內的任何一個」,以數字為例,[0-9]代表這個字元可以是 0~9 之間的任意數字,如果是 [a-z] 則代表是小寫字母 a~z 之間的任意文字,聰明的你,應該可以猜出 [A-Z] 代表的是甚麼意思吧?
5054

51-
# In[26]:
55+
# In[3]:
56+
57+
import re
5258

5359
pattern = "[0-9]+"
5460
test_string = '12 drummers drumming, 11 pipers piping, 10 lords a-leaping'
@@ -58,7 +64,9 @@
5864
# ### 練習 02-2: 找到英數字
5965
# 在 test_string 中找出所有數字
6066

61-
# In[21]:
67+
# In[4]:
68+
69+
import re
6270

6371
# your codes
6472
pattern = "[1-3]+"
@@ -69,14 +77,18 @@
6977
# ### 範例 02-3: 找到文字
7078
# 當有指定的文字需要搜尋,可透過 [ ] 搭配 *, + ,{} 進行搜尋
7179

72-
# In[28]:
80+
# In[5]:
81+
82+
import re
7383

7484
pattern = "[cmf]an"
7585
test_string = 'find: can, man, fan, skip: dan, ran, pan'
7686
re.findall(pattern, test_string)
7787

7888

79-
# In[29]:
89+
# In[6]:
90+
91+
import re
8092

8193
pattern = "jim{2,5}y"
8294
test_string = 'find: jimmy, jimmmy, jimmmmmy, skip: jimy'
@@ -88,7 +100,9 @@
88100
#
89101
# Hint: 如果只找到一個大寫字母,想想甚麼符號代表可出現一次以上?
90102

91-
# In[22]:
103+
# In[7]:
104+
105+
import re
92106

93107
# your codes
94108
pattern = "[A-Z]+[a-z]"
@@ -103,7 +117,9 @@
103117
#
104118
# 這時在 "+" 前面加上 "\" (跳脫符號),這樣做的話 regular expression 就會知道你是要尋找 "+"
105119

106-
# In[31]:
120+
# In[8]:
121+
122+
import re
107123

108124
pattern = ".{3}\."
109125
test_string = 'find: 591., dot., yes., skip: non!'
@@ -113,7 +129,9 @@
113129
# ### 練習 02-4: 跳脫符號
114130
# 在 test_string 中找到 A+c, B+d, C+x
115131

116-
# In[23]:
132+
# In[9]:
133+
134+
import re
117135

118136
# your codes
119137
pattern = "[A-Z]\+[a-z]"
@@ -124,7 +142,9 @@
124142
# ### 範例 02-5: 條件式搜尋
125143
# 當希望不同的搜尋條件都能夠符合時,可以使用「|」這個符號,代表左右邊只要任一一個條件符合,就會回傳
126144

127-
# In[33]:
145+
# In[10]:
146+
147+
import re
128148

129149
pattern = "I love cats|I love dogs"
130150
test_string = 'find: I love cats, I love dogs, skip: I love logs, I love cogs'
@@ -134,7 +154,9 @@
134154
# ### 練習 02-5: 條件式搜尋
135155
# 在 test_string 中找到 jimy, jimmmy, 但不包含 jimmy, jimmmmy
136156

137-
# In[24]:
157+
# In[11]:
158+
159+
import re
138160

139161
# your codes
140162
pattern = "jimy|jim{3}y"
@@ -144,7 +166,9 @@
144166

145167
# ### 範例 02-6: Email 搜尋
146168

147-
# In[35]:
169+
# In[12]:
170+
171+
import re
148172

149173
email_text = """
150174
Big Data Analytics/ Deep LearningSocial Computing / Computational Social Science / Crowdsourcing
@@ -159,7 +183,9 @@
159183
"""
160184

161185

162-
# In[36]:
186+
# In[13]:
187+
188+
import re
163189

164190
re.findall("([A-Za-z0-9._]+@[A-Za-z.]+(com|edu)\.tw)", email_text)
165191

@@ -174,33 +200,35 @@
174200
# * text = " ".join(text_list),這段 code 可以將 list of string 全部變為一個字串
175201
# * 變成字串後就可以用剛剛學的 re.findall() 找出我們要的目標囉!
176202

177-
# In[37]:
203+
# In[14]:
178204

179205
# 如果忘記怎麼寫 requests 或 BeautifulSoup,可以參考
180206

181-
# response = requests.get("http://yp.518.com.tw/service-life.html?ctf=10")
207+
# response = requests.get("https://jimmy15923.github.io/518")
182208
# print(response.encoding)
183209

184210
# soup = BeautifulSoup(response.text, "lxml")
185211

186212

187-
# In[2]:
213+
# In[15]:
188214

189215
# your codes
190216
import requests
191217
from bs4 import BeautifulSoup
218+
import re
192219

193-
response = requests.get("http://yp.518.com.tw/service-life.html?ctf=10") # requests 518 網頁 並拿到 response
220+
## 518 網頁伺服器無法容納多人同時 requests,請大家使用以下的網頁作 requests,其 html 的內容是一模一樣的
221+
response = requests.get("https://jimmy15923.github.io/518") # requests 518 網頁 並拿到 response
194222
print(response.encoding) # 印出 encoding 結果
195223
soup = BeautifulSoup(response.text, "lxml") # 將 HTML 丟給 BeautifulSoup 作解析
196224

197-
198-
# In[ ]:
199-
225+
# 找到電話資訊都藏在 li 標籤,屬性 class=comp_tel
200226
all_phone_text = [tag.text for tag in soup.find_all("li",class_="comp_tel")]
201227

228+
# 把電話資訊的 list 存成一個大 string
202229
all_phone_text ="".join(all_phone_text)
203230

231+
# 用 regular expression 把全部的電話都找出來
204232
phone_number = re.findall("0[1-9]+-[0-9]+", all_phone_text)
205233
print(phone_number)
206234

Session_A/practice/01_BeautifulSoup_functions.ipynb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,10 @@
226226
"\n",
227227
"# 2. 發送 requests.get,並將結果存在 response (或自己定義喜歡的變數也可以)\n",
228228
"# your codes\n",
229-
"response = \n",
229+
"\n",
230230
"\n",
231231
"# 3. 將 response 的 HTML 文字放進 BeautifulSoup,並將結果存在 soup (或自己定義喜歡的變數也可以)\n",
232-
"# your codes\n",
233-
"soup = "
232+
"# your codes\n"
234233
]
235234
},
236235
{
@@ -276,7 +275,7 @@
276275
"cell_type": "markdown",
277276
"metadata": {},
278277
"source": [
279-
"Q3. 請找出**列3欄3**背後的超連結網址? (請使用 BeautifulSoup + 右鍵→檢查 來找到那個標籤,不要偷偷從網頁點開連結來看喔^^)"
278+
"Q3. 請找出**列3欄3**背後的超連結網址? (請使用右鍵→檢查 來找到列3欄3的標籤,不要偷偷從網頁點開連結來看喔^^)"
280279
]
281280
},
282281
{

Session_A/practice/02_regular_expression.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"metadata": {},
66
"source": [
77
"## 範例 02: regular expression\n",
8-
"regular expression 是在搜尋大量文字時非常好用的工具,可以快速回傳符合您要求的文字\n",
8+
"regular expression 是在大量文字中蒐尋目標資訊時非常好用的工具,可以快速回傳符合您要求的文字\n",
99
"\n",
1010
"例如尋找任何像是電話號碼、E-mail 信箱的文字\n",
1111
"\n",
@@ -75,7 +75,7 @@
7575
"### 練習 02-1: *, +, {} 的用法\n",
7676
"在 test_string 中找出 abbbbc, bc,但不包含 c, acc\n",
7777
"\n",
78-
"Hint: 思考一下要尋找的文字跟要濾除的文字,在字母之間有甚麼差異,先把 find 寫出來,再想辦法去掉要 skip "
78+
"Hint: 思考一下要尋找的文字跟要濾除的文字,在字母之間有甚麼差異"
7979
]
8080
},
8181
{
@@ -432,7 +432,8 @@
432432
"source": [
433433
"# 如果忘記怎麼寫 requests 或 BeautifulSoup,可以參考\n",
434434
"\n",
435-
"# response = requests.get(\"http://yp.518.com.tw/service-life.html?ctf=10\")\n",
435+
"## 518 網頁伺服器無法容納多人同時 requests,請大家使用以下的網頁作 requests,其 html 的內容是一模一樣的\n",
436+
"# response = requests.get(\"https://jimmy15923.github.io/518\")\n",
436437
"# print(response.encoding)\n",
437438
"\n",
438439
"# soup = BeautifulSoup(response.text, \"lxml\")"

0 commit comments

Comments
 (0)