-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler_old.py
More file actions
48 lines (42 loc) · 1.45 KB
/
crawler_old.py
File metadata and controls
48 lines (42 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import requests
import csv
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('voicetube.html'), 'lxml')
word_list = []
for ele in soup.select('.show_control'):
id = ele.get('word_id')
word = []
for ele2 in ele.select('td:nth-child(2) a'):
word.append(ele2.string)
word.append('')
word.append('')
word.append('')
for ele2 in ele.select('td:nth-child(5) b'):
word.append(ele2.string)
for ele2 in soup.find_all('small', word_id=id):
isHighLight = ele2.select('.highlight')
isStarIcon = ele2.select('.icon-star')
if len(isHighLight) > 0:
sentance = []
for idx, ele3 in enumerate(ele2.strings):
if idx <= 2:
sentance.append(str(ele3))
else:
word.append("".join(sentance))
word.append(str(ele3))
elif len(isStarIcon) > 0:
sentance = []
for idx, ele3 in enumerate(ele2.strings):
if idx <= 5:
sentance.append(str(ele3))
else:
word.append("".join(sentance))
word.append(str(ele3))
else:
for ele3 in ele2.strings:
word.append(str(ele3))
word_list.append(word)
with open('output.csv', 'w', newline='') as csvfile:
# 建立 CSV 檔寫入器
writer = csv.writer(csvfile, delimiter=';')
writer.writerows(word_list)