forked from PyThaiNLP/thaigov-corpus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·31 lines (31 loc) · 1.01 KB
/
run.py
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
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import codecs
import re
i=10155
i2=1
data={}
while i<=10156:
url="http://www.thaigov.go.th/news/contents/details/"+str(i)
try:
r = requests.get(url)
print(i)
if(r.status_code==200):
title = re.search('<title>(.*?)</title>',r.text).group(1) #soup.title.text
if title!="รัฐบาลไทย-ข่าวทำเนียบรัฐบาล-":
soup = BeautifulSoup(r.text, "lxml")
article = soup.find('div',{'class':'border-normal clearfix'}).text #soup.article.text
collection=soup.find('span',{'class':'Circular headtitle-2 font_level6 color2 col-xs-9 remove-xs'}).text
all=title+"\n\n"+article+"\n\nที่มา : "+url
if collection not in data:
data[collection]=1
with codecs.open(collection+"_"+str(data[collection])+".txt", "w", "utf-8-sig") as temp:
temp.write(all)
temp.close()
data[collection]+=1
print("ok")
i2+=1
i+=1
except:
print("error")