๊ฐ์ธ๊ณต๋ถ/Python
77. Python TextMining ์ฐ์ต๋ฌธ์ (1)
LEE_BOMB
2021. 12. 11. 22:48
๋ฌธ1) member.html ์น ๋ฌธ์๋ฅผ ๋์์ผ๋ก ๋ค์ ์กฐ๊ฑด์ ๋ง๊ฒ ๋ด์ฉ์ ์ถ์ถํ์์ค.
<์กฐ๊ฑด> <tr> ํ๊ทธ ํ์ ํ๊ทธ์ธ <td> ํ๊ทธ์ ๋ชจ๋ ๋ด์ฉ ์ถ๋ ฅ
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
์์ด๋
hong123
๋น๋ฐ๋ฒํธ
1234
์ด๋ฆ
ํ๊ธธ๋
from bs4 import BeautifulSoup
1. ํ์ผ ์ฝ๊ธฐ
file = open("C:/ITWILL/4_Python-2/workspace/chap10_TextMining/data/member.html", mode='r', encoding='utf-8')
source = file.read()
file.close()
2. html ํ์ฑ
html = BeautifulSoup(source, 'html.parser')
3. ํ๊ทธ ์ฐพ๊ธฐ
tds = html.find_all('td') #td ํ๊ทธ ์ ์ฒด ์ฐพ๊ธฐ
print(tds) #list ๋ฐํ
[<td> ์์ด๋ </td>, <td> hong123 </td>, <td> ๋น๋ฐ๋ฒํธ </td>, <td> 1234 </td>, <td> ์ด๋ฆ </td>, <td> ํ๊ธธ๋ </td>]
4. ํ๊ทธ ๋ด์ฉ ์ถ๋ ฅ
contents = [td.text for td in tds] #td ํ๊ทธ ๋ด์ฉ ์ถ์ถ
print(contents)
print('<์ถ๋ ฅ๊ฒฐ๊ณผ>')
for c in contents :
print(c)
๋ฌธ2) urls์ url์ ๋์์ผ๋ก ๋ค์ ์กฐ๊ฑด์ ๋ง๊ฒ ์น ๋ฌธ์์ ์๋ฃ๋ฅผ ์์งํ์์ค.
์กฐ๊ฑด1> http://์ผ๋ก ์์ํ๋ url๋ง์ ๋์์ผ๋ก ํ๋ค.
์กฐ๊ฑด2> url์ ํด๋นํ๋ ์น ๋ฌธ์๋ฅผ ๋์์ผ๋ก <a> ํ๊ทธ(tag) ๋ด์ฉ์ ์ถ๋ ฅํ๋ค.
from urllib.request import urlopen #ํจ์ : ์๊ฒฉ ์๋ฒ url ์์ฒญ
from bs4 import BeautifulSoup #ํด๋์ค : html ํ์ฑ
import re #์ ๊ทํํ์
urls = ['http://www.daum.net', 'www.daum.net', 'http://www.naver.com']
๋จ๊ณ1 : url ์ ์
new_urls = []
for url in urls :
tmp = re.findall('^http://', url)
if tmp :
new_urls.append(url)
๋จ๊ณ2 : url์์ a ํ๊ทธ ๋ด์ฉ ์์ง & ์ถ๋ ฅ
for url in new_urls :
#1. url ์์ฒญ
print('url :', url)
req = urlopen(url)
data = req.read()
#2. html ํ์ฑ
src = data.decode('utf-8')
html = BeautifulSoup(src, 'html.parser')
#3. a ํ๊ทธ ์ฐพ๊ธฐ & ๋ด์ฉ
a_all = html.find_all('a') #์ต์ปค ํ๊ทธ ์ ์ฒด ์ฐพ๊ธฐ
print('a ํ๊ทธ ์ ์ฒด ๊ฐ์ :', len(a_all)) #a ํ๊ทธ ์ ์ฒด ๊ฐ์ : 414
for a in a_all :
print(a.text) #tag ๋ด์ฉ ์ถ๋ ฅ
๋ฌธ3) ์๋ url์ ์ด์ฉํ์ฌ ์ด๋ฆฐ์ด๋ (20210505)์ ์ ๊ณต๋ ๋ด์ค ๊ธฐ์ฌ๋ฅผ 1~5ํ์ด์ง ํฌ๋กค๋งํ๋ ํฌ๋กค๋ฌ ํจ์๋ฅผ ์ ์ํ๊ณ ํฌ๋กค๋ง ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์์ค.
base_url = "https://news.daum.net/newsbox?regDate="
<์กฐ๊ฑด1> ํฌ๋กค๋ฌ ํจ์์ ํ๋ผ๋ฏธํฐ(page๋ฒํธ, ๋ ์ง)
<์กฐ๊ฑด2> ํฌ๋กค๋ง ๋์ : <a> ํ๊ทธ์ 'class=link_txt' ์์ฑ์ ๊ฐ๋ ๋ด์ฉ
<์กฐ๊ฑด3> ํฌ๋กค๋ง ๊ฒฐ๊ณผ ํ์ธ : news ๊ฐ์์ news ์ถ๋ ฅ
import urllib.request as req # url ๊ฐ์ ธ์ค๊ธฐ
from bs4 import BeautifulSoup
ํด๋ก๋ฌ ํจ์(ํ์ด์ง์, ๊ฒ์๋ ์ง)
def crawler_func(pages, date):
base_url = "https://news.daum.net/newsbox?regDate="
crawling_news = [] #5 page news ์ ์ฅ
url = base_url + date
#url = https://news.daum.net/newsbox?regDate=20210505
#page ๋จ์ news ์์ง
for page in range(1, pages+1) : #1~5 page
p = '&page=' + str(page)
url += p #url = url + p
#url = https://news.daum.net/newsbox?regDate=20210505&page=1
#1. url ์์ฒญ
res = req.urlopen(url)
src = res.read() #source
data = src.decode('utf-8') #๋์ฝ๋ฉ ์ ์ฉ
#2.html ํ์ฑ
html = BeautifulSoup(data, 'html.parser')
#3. tag ์์ ์ถ์ถ
#1) tag element ์์ง
a_tag = html.select('a[class="link_txt"]')
#2) ์๋ฃ ์์ง
page_news = [] #1 page news ์ ์ฅ
for a in a_tag :
cont = str(a.string) #๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ -> ๋ฌธ์์ด
page_news.append(cont.strip())
crawling_news.extend(page_news[:40]) #5 page news ์ ์ฅ
return crawling_news
ํด๋ก๋ฌ ํจ์ ํธ์ถ
crawling_news = crawler_func(5, '20210505') #(ํ์ด์ง์, ๊ฒ์๋ ์ง)
print('ํฌ๋กค๋ง news ๊ฐ์ =', len(crawling_news)) #200=5*40
print('ํฌ๋กค๋ง news')
print(crawling_news)