-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfour.py
More file actions
60 lines (48 loc) · 2.69 KB
/
four.py
File metadata and controls
60 lines (48 loc) · 2.69 KB
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
import requests, operator, pandas, glob2
from bs4 import BeautifulSoup
from datetime import datetime
# 크롤링 함수(날짜와 페이지수를 입력받아 그날짜의 그 페이지수만큼 크롤링 해옴)
def crawlingData(date, pageCount):
# 현재 시각을 now라는 변수에 저장
now = datetime.now()
l = [] # 리스트 lc
# pagecount는 1페이지부터 사용자가 입력한 페이지 수까지 됨
for pagecount in range(1, int(pageCount)):
# 동적으로, 사용자가 입력한 날짜와 뉴스 페이지에 접속
r = requests.get("http://news.naver.com/main/list.nhn?mode=LSD&mid=sec&sid1=100&date=" + str(date) + "&page=" + str(pagecount))
c = r.content
soup = BeautifulSoup(c, "html.parser")
all = soup.find_all("li")
for item in all:
for item2 in item.find_all("dl"):
d = {} # 사전 d
try:
linkTag = item2.find("dt", {"class": ""}).find("a")
d["LinkSrc"] = linkTag['href'] # 사전 d의 LinkSrc라는 키에 href 내용을 가져와 저장
d["Title"] = linkTag.text.replace("\t", "").replace("\n", "").replace(",", "").replace('"',"").replace("\r", "")[1:len(linkTag.text) + 1]
except:
d["LinkSrc"] = "None"
d["Title"] = "None"
try:
contentTag = item2.find("dd")
d["Content"] = contentTag.find("span",{"class":"lede"}).text
contentTag.text.replace("\t", "").replace("\n", "").replace("\r", "").replace(",", "").replace('"',"").split("…")
d["Company"] = contentTag.find("span", {"class": "writing"}).text
d["Date"] = contentTag.find("span", {"class": "date"}).text
print(d["Content"])
except:
d["Content"] = "None"
d["Company"] = "None"
d["Date"] = "None"
try:
imgTag = item2.find("dt", {"class": "photo"}).find("img")
d["imgSrc"] = imgTag["src"]
except:
d["imgSrc"] = "No image"
l.append(d) # 리스트에 사전 추가 / 한 행마다 사전에 추가
df = pandas.DataFrame(l) # pandas 사용 l의 데이터프레임화
# now(현재시각)을 이용해 csv 파일로 저장
df.to_csv('%s-%s-%s-%s-%s-%s.csv' % (now.year, now.month, now.day, now.hour, now.minute, now.second),
encoding='utf-8-sig', index=False)
print("Success Get DataFIle and Save Data")
crawlingData(20191026, 3)