하고재비

[PYTHON] 웹크롤러 본문

Python

[PYTHON] 웹크롤러

DeadDE 2017. 12. 15. 09:42
기사내용 TXT 저장
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
from urllib.request import urlopen
from bs4 import BeautifulSoup
 
html = urlopen('http://news.naver.com/main/read.nhn?mode=LSD&mid=shm&sid1=103&sid2=245&oid=020&aid=0003113573')
# html = urlopen('file:///C:/Users/USER/Desktop/songsong.html')
#html = urlopen('https://dak.gg/profile/DeadDE/2017-pre6/krjp')
soup = BeautifulSoup(html, 'lxml')
 
#nameList = soup.find_all('span', {'class': 'red'})
nameList = soup.find_all('h3' ,{'id':'articleTitle'})
nameList_1 = soup.find_all('div' ,{'id':'articleBodyContents'})
#nameList_1 = soup.find_all('span',{'class':'top'})
'''
for name in nameList_1:
    print(name.get_text())
'''
 
 
= open("저장할 경로 /새파일.txt"'w')
for name in nameList:
    f.write("기사제목 : " + name.get_text())
 
for name_1 in nameList_1:
    f.write(name_1.get_text())
f.close()
cs


'Python' 카테고리의 다른 글

[python] 이미지 크롤링 + 경로 저장  (0) 2018.02.08
[PYTHON] 크롤링  (0) 2017.12.08
[python] list 선언  (0) 2017.12.01
[python] 디렉토리에 특정파일 찾기  (0) 2017.12.01
[python]꺽은선 그래프  (0) 2017.12.01
Comments