-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.py
42 lines (39 loc) · 1.28 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
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import codecs
import re
i=21654
i2=1
data={}
headers = {'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'}
while True:
url="https://www.thaigov.go.th/news/contents/details/"+str(i)
try:
r = requests.get(url, headers=headers, timeout=60)
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
_text = ''
for line in article.split('\n'):
line = line.strip()
if line:
_text = _text + '\n' + line
article = _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")
i+=1