Crawling

(Web Crawling) ์ „๊ตญ ๋ชจ๋“  ๋Œ€ํ•™๊ต ์ด๋ฆ„ ์ถœ๋ ฅํ•˜๊ธฐ with Python

hyunjun's developing ๐Ÿฃ 2024. 3. 2. 18:12

์ •๋ง ์˜ค๋žœ๋งŒ์— ๋ธ”๋กœ๊ทธ๋ฅผ ์“ฐ๋Š” ๊ฑฐ ๊ฐ™๋‹ค. ํ•‘๊ณ„๋ฅผ ๋Œ€๋ณด์ž๋ฉด ์š”์ฆ˜ ์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ ํ•„๊ธฐ ์‹œํ—˜, ๊ฐœ์ธ ํฌํŠธํด๋ฆฌ์˜ค ์ค€๋น„, ์กธ์—… ์ค€๋น„ ๋“ฑ๋“ฑ์œผ๋กœ ๋งŽ์ด ์‹œ๊ฐ„์ด ์—†์—ˆ๋‹ค. ์•”ํŠผ ! ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ํฌ๋กค๋ง์ด๋‹ค. ํฌ๋กค๋ง์— ๊ด€์‹ฌ์ด ์žˆ์–ด์„œ ๊ด€๋ จ ๋‚ด์šฉ์„ ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ณด๋ฉฐ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ์•Œ๊ฒŒ ๋œ ๋‚ด์šฉ๊ณผ ์˜ค๋ฅ˜, ๋‚ด๊ฐ€ ์ˆ˜์ •ํ•œ ๋ถ€๋ถ„์„ ์จ๋ณด๋ ค๊ณ  ํ•œ๋‹ค. 

 

๋จผ์ € ๋‚ด๊ฐ€ ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ์˜ ์ถœ์ฒ˜๋ฅผ ๋‚จ๊ธฐ๊ฒ ๋‹ค.

 

https://well-made-codestory.tistory.com/33#03

 

[Web Crawling] ์ „๊ตญ ๋Œ€ํ•™๊ต ์ด๋ฆ„ ํฌ๋กค๋ง ํ•˜์—ฌ DB์— ์ €์žฅํ•˜๊ธฐ -1

์ „๊ตญ ๋Œ€ํ•™๊ต ์ด๋ฆ„์„ ํฌ๋กค๋ง ํ•˜์—ฌ DB์— ์ €์žฅํ•˜๊ธฐ -1 ๊ฐœ์š” Toy Project์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ „๊ตญ์˜ ๋Œ€ํ•™๊ต๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅํ•˜์—ฌ DB์— ์ •๋ฆฌํ•˜๊ณ , ์ด๋ฅผ ํšŒ์›๊ฐ€์ž…์‹œ์— ์ €์žฅ๋œ DB๋ฅผ ๋ฏธ๋ฆฌ ๋…ธ์ถœ ์‹œ์ผœ ํ†ต์ผ๋œ ์–‘์‹์œผ

well-made-codestory.tistory.com

 

 

 

1
2
import cloudscraper
from bs4 import BeautifulSoup
cs

 

 

cloudscraper๋Š” ํฌ๋กค๋งํ•  ๋–„ ํด๋ผ์šฐ๋“œ ๋ณดํ˜ธ๋ฅผ ์šฐํšŒํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

BeautifulSoup๋Š” HTML์ด๋‚˜ XML์—์„œ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ํŒŒ์‹ฑํ•  ๋•Œ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

 

 

1
2
3
4
5
6
7
8
def get_html(url):              #html ํŽ˜์ด์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
    scraper = cloudscraper.create_scraper()
    html = ""
    g_html = scraper.get(url)
    if g_html.status_code == 200:
        html = g_html.text
        
    return html
cs

 

 

์ด ๋ถ€๋ถ„์€ html ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์ด๋‹ค. 

 

cloudscraper.create_scraper()๋Š” cloudscraper์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค. 

 

 

 

1
2
3
4
5
6
7
8
9
10
def crawl_html(html):           #๊ฐ€์ ธ์˜จ html ํŽ˜์ด์ง€์˜ tag ์† ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
    u_list = []
    soup_html = BeautifulSoup(html,'html.parser')
    title = soup_html.find_all('a', class_= "A4OyphEg")
    for t in title:
        tmp = t["title"]
        if '๋ชฉ๋ก' not in tmp:
            u_list.append(tmp)
    s_list = set(u_list)
    return s_list
cs

 

์ด ๋ถ€๋ถ„์—์„œ ํฌ๋กค๋ง์„ ๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹ค์‹œํ•œ๋‹ค. ๋จผ์ € ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ๋‘”๋‹ค. ๊ทธ ๋‹ค์Œ BeautifulSoup ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทธ ๋‹ค์Œ ๊ทธ ๊ฐ์ฒด์˜ find_all ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ a ํƒœ๊ทธ์— class๊ฐ€ "A40yphEg"์ธ ๊ฒƒ์„ ๋ชจ๋‘ ๊ฐ€์ ธ์˜จ๋‹ค. ์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ์ˆ˜์ •ํ•œ ๊ฒƒ์ด ๋ฐ˜์˜์ด ๋๋Š”๋ฐ ํด๋ž˜์Šค๊ฐ€ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— HTML์ •๋ณด๋ฅผ ํ™•์ธํ•ด๋ณด๊ณ  ํด๋ž˜์Šค๋ฅผ ๋ฐ”๊ฟ”์„œ ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ์ด ๋œ๋‹ค. ์šฐ๋ฆฌ๋Š” ์žก์ •๋ณด๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ๊ณ  ์ด๋ฆ„๋งŒ์„ ์›ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— title ์†์„ฑ๋งŒ์„ ์ถ”์ถœํ•ด์„œ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค. 

 

ใ„ฑ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ set()์œผ๋กœ ์ค‘๋ณต๋œ ๊ฒƒ์„ ์ œ๊ฑฐํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๊ฒƒ์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

 

 

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
import cloudscraper
from bs4 import BeautifulSoup
 
def get_html(url):              #html ํŽ˜์ด์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ
    scraper = cloudscraper.create_scraper()
    html = ""
    g_html = scraper.get(url)
    if g_html.status_code == 200:
        html = g_html.text
        
    return html
 
def crawl_html(html):           #๊ฐ€์ ธ์˜จ html ํŽ˜์ด์ง€์˜ tag ์† ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
    u_list = []
    soup_html = BeautifulSoup(html,'html.parser')
    title = soup_html.find_all('a', class_= "A4OyphEg")
    for t in title:
        tmp = t["title"]
        if '๋ชฉ๋ก' not in tmp:
            u_list.append(tmp)
    s_list = set(u_list)
    return s_list
 
html = get_html('https://namu.wiki/w/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD%EC%9D%98%20%EB%8C%80%ED%95%99%EA%B5%90%20%EB%AA%A9%EB%A1%9D')
univ_list = crawl_html(html)
univ_list = list(univ_list)
univ_list.sort()
for univ in univ_list:
    print(univ, end='\n')
 
 
cs

 

 

 

์ „์ฒด ์ฝ”๋“œ์ด๋‹ค.

 

 

 

.