Knowledge Map

크롤링_python2 본문

PYTHON

크롤링_python2

2016. 4. 11. 10:57
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
#-*- coding: utf-8 -*-  #한글을 쓸 때는 꼭 붙인다. 문자 인코딩을 UTF-8로 하겠다는 것이다. 인코딩은 앞으로 계속 속썩일 것이다.
 
import urllib #URL을 열고 HTML을 읽는 모듈, urllib을 불러온다
from bs4 import BeautifulSoup #bs4모듈에서 뷰티풀수프 함수를 불러옴
import re
from datetime import datetime
 
 
targetUrl = "http://naver.com"
soup = BeautifulSoup(urllib.urlopen(targetUrl).read()) #해당 웹주소 열고 뷰티풀수프로 긁어온 다음 soup라는 변수에 넣는다.
 
 
editData = soup.find_all('a', {'class'"newssa"})
article = list(editData)
saveadr = []
savedic = {}
hangul = re.compile('[^ ㄱ-ㅣ가-힣]+')
 
# hanPatter = re.search('[^ ㄱ-ㅣ가-힣]+',test1) #search에는 인수가 2개이어야 한다.
number = 0
 
# dic 타입으로 저장해보자.
newsSite = {}
 
 
for a in article:
    test1 = str(a)
 
    # htest = test1[test1.find('alt='):test1.find('src')].replace('alt=','').replace('"','')
    newsSiteName = test1[test1.find('alt='):test1.find('src')].replace('alt=','').replace('"','')
    # 정규표현식으로 한글을 골라 내려고 했지만 그렇게 하면 영어 sbs등이 생략되기 때문에 find, slice, replace를 이용함
    newsSiteUrl = test1[test1.find('http'):test1.find('" target')]
 
    newsSite[newsSiteName] = newsSiteUrl
    number +=1
 
    # saveadr = test1.split('<img alt=')[0]
 
 
 
num = 0
testAdr = []
for i in newsSite:
    testTxt = BeautifulSoup(urllib.urlopen(newsSite[i]).read())
 
    testEdit = testTxt.find_all('a', {'class':'btn_gomedia'})
    for j in testEdit:
        strJ = str(j)
        testAdr.append((strJ[strJ.find('<strong>'):strJ.find('</strong>')].replace('<strong>','')+'::'+strJ[strJ.find('ht'):strJ.find('" target')]).split('::'))
 
 
for i in testAdr:
    print testAdr[num][0],'::',testAdr[num][1]
    num+=1
 
 
cs


실행 결과


광주일보 :: http://www.kwangju.co.kr

한국일보 :: http://www.hankookilbo.com/

스포탈코리아 :: http://sportalkorea.com

충청투데이 :: http://cctoday.co.kr

코리아헤럴드 :: http://www.koreaherald.com/index_kr.php

코리아타임스 :: http://www.koreatimes.co.kr/www/index_kr.asp

경북일보 :: http://www.kyongbuk.co.kr

프레시안 :: http://www.pressian.com/

OSEN :: http://osen.mt.co.kr/

경상일보 :: http://ksilbo.co.kr/

디지털타임스 :: http://www.dt.co.kr/

YTN :: http://www.ytn.co.kr/

강원도민일보 :: http://www.kado.net/

문화일보 :: http://www.munhwa.com/

서울신문 :: http://www.seoul.co.kr/

경북매일신문 :: http://www.kbmaeil.com

서울신문 :: http://www.seoul.co.kr/

한겨레 :: http://www.hani.co.kr/

강원일보 :: http://www.kwnews.co.kr/

OSEN :: http://osen.mt.co.kr/

오마이뉴스 :: http://www.ohmynews.com/

인천일보 :: http://www.incheonilbo.com/

매일경제 :: http://www.mk.co.kr/

한국경제TV :: http://www.wowtv.co.kr/

전북일보 :: http://www.jjan.kr

스포탈코리아 :: http://sportalkorea.com

데일리안 :: http://www.dailian.co.kr/

강원일보 :: http://www.kwnews.co.kr/

OSEN :: http://osen.mt.co.kr/

오마이뉴스 :: http://www.ohmynews.com/

전남일보 :: http://www.jnilbo.com

프레시안 :: http://www.pressian.com/

MBC :: http://imnews.imbc.com/

충청투데이 :: http://cctoday.co.kr

미디어오늘 :: http://www.mediatoday.co.kr/

한겨레 :: http://www.hani.co.kr/


추후에 이렇게 얻은 웹사이트 주소에서 다시 크롤링하는거 해보기

'PYTHON' 카테고리의 다른 글

파이썬 포함, 상속 문제  (0) 2016.04.16
pip upgrade  (0) 2016.04.16
텍스트 파일 읽어오기 및 슬라이싱  (0) 2016.04.10
파이썬 강의 필기 2일차  (0) 2016.04.10
파이썬 한글 정규표현식  (0) 2016.04.05
Comments