forked from taenyun93/GoEat
/
GoEatWebCrawler.py
152 lines (107 loc) · 4.45 KB
/
GoEatWebCrawler.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import requests
from bs4 import BeautifulSoup as BS
import re
from collections import Counter
from konlpy.tag import Hannanum
from konlpy.tag import Kkma
from konlpy.utils import concordance, pprint
class GoEatWebCrawler:
def recipe_finder(self,foodname,num_recipe=3):
#get food name as keyword
keyword = foodname
#convert keyword into unicode
keyword = str(keyword.encode('utf-8')).lstrip("b'").rstrip("'").replace("\\x","%")
# get url
url = 'http://www.10000recipe.com/recipe/list.html?q=' + keyword
#connect to the site
response = requests.get(url)
html = response.text
#parse using BS
soup = BS(html,'lxml')
#get cookbook link
if soup.find("a","thumbnail") == None:
recipe = ''
recipe_list = []
else:
cook_link_list = soup.find_all('a',"thumbnail")
cook_urls = []
for cook_links in cook_link_list:
cook_link =cook_links['href']
cook_urls.append('http://www.10000recipe.com' + cook_link)
cook_urls = cook_urls[0:num_recipe]
#connect to the site
recipe_list = []
for cook_url in cook_urls:
response = requests.get(cook_url)
html = response.text
#parse using BS
soup2 = BS(html,'lxml')
contents = soup2.find_all('meta',{'name':"keywords"})
#get recipe
recipe_content = contents
recipe = ''
for rec in recipe_content:
recipe = recipe +str(rec['content'])
#replace useless tokens
#recipe = recipe.replace('text/html; charset=euc-kr','')
#recipe = recipe.replace('\r\n','')
#recipe = re.sub('http.+','',recipe)
recipe_list.append(recipe)
return recipe_list
def crawl(self,food_detail_df):
print('[Recipe Web Crawling Start]')
for index in range(len(food_detail_df)):
food = food_detail_df.foodName[index]
recipe = self,recipe_finder(food,2)
food_detail_df.loc[index ,'foodRecipe'] = str(recipe)
if (index+1) % 5 == 0:
print(round((index+1)/len(food_df)*100,2),'percent Done')
print('Complete!!')
print('')
print('[noun extract start]')
food_detail_df['foodRecipeNoun'] = ''
for i in range(len(food_detail_df)):
doc = food_detail_df.foodRecipe[i]
noun = Hannanum().nouns(doc)
cnt = Counter(noun)
only_word = []
for key, value in cnt.items():
if int(value) < 3:
noun.remove(key)
for word in noun:
m = re.match('^\D*\D$',word)
if m:
only_word.append(m.group())
food_detail_df.loc[i,'foodRecipeNoun'] = str(only_word)
if (i % 5) == 0:
print(round(i/len(food_detail_df)*100,2), ' perent done')
print('Complete')
def NLP(self,food_detail_df):
print('[noun extract start]')
food_detail_df['foodRecipeNoun'] = ''
for i in range(len(food_detail_df)):
doc = food_detail_df.foodRecipe[i]
noun = Hannanum().nouns(doc)
for word in noun:
word = word.replace('ㅎ','').replace('ㅋ','').replace('ㅜㅜ','').replace('ㅠㅠ','').replace('\\n','')
cnt = Counter(noun)
only_word = []
for key, value in cnt.items():
#if (len(key) < 2)|(len(key) > 6):
#noun.remove(key)
if int(value) < 3:
noun.remove(key)
for word in noun:
m = re.match('^\D*\D$',word)
if m:
only_word.append(m.group())
food_detail_df.loc[i,'foodRecipeNoun'] = str(only_word)
if (i % 5) == 0:
print(round(i/len(food_detail_df)*100,2), ' perent done')
print('Complete')
def replace_useless(self,food_detail_df):
count=0
for word in food_detail_df['foodRecipeNoun']:
food_detail_df['foodRecipeNoun'][count] = word.replace("[","").replace("]","").replace("'","").replace(","," ")\
.replace("ㅎ","").replace("ㅋ","").replace("ㅜ","").replace("n","")
count = count + 1