File size: 9,698 Bytes
a6d7194 b2dbbaf 57c4050 a6d7194 0fc522e 57c4050 0fc522e 57c4050 ec13f7a 57c4050 a6d7194 ec13f7a a6d7194 2296ffd a6d7194 57c4050 b2dbbaf 57c4050 a6d7194 57c4050 a6d7194 0fc522e 57c4050 a6d7194 57c4050 b2dbbaf a6d7194 57c4050 0fc522e |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import uuid
import time
import urllib.request
from lxml import etree
from datetime import datetime, timedelta
from utils import encode, translate, datemodifier, sentiment_computation, upsert_content
i = 0
while i > -1:
if i == 0:
CATEGORY_URL = "https://www.mof.gov.cn/zhengwuxinxi/caizhengxinwen/"
else:
CATEGORY_URL = f"https://www.mof.gov.cn/zhengwuxinxi/caizhengxinwen/index_{i}.htm"
i = i + 1
req = urllib.request.urlopen(CATEGORY_URL)
text = req.read()
html_text = text.decode("utf-8")
page = etree.HTML(html_text)
articlelist = page.xpath("//div[contains(@class, 'xwfb_listerji')]/ul/li[not(@class = 'clear')]")
for article in articlelist:
if isinstance(article, etree._Element):
subelement = etree.tostring(article).decode()
subpage = etree.HTML(subelement)
date = subpage.xpath("//span/text()")[0]
parsed_datetime = datetime.strptime(time.strftime("%Y-%m-%d", time.strptime(date,"%Y-%m-%d")), "%Y-%m-%d")
if parsed_datetime < (datetime.today() - timedelta(days=183)):
i = -1
else:
urls = subpage.xpath("//a[contains(@target, '_blank')]/@href")
for url in urls:
try:
article = {}
url = url.replace("../", "https://www.mof.gov.cn/zhengwuxinxi/")
url = url.replace("./", "https://www.mof.gov.cn/zhengwuxinxi/caizhengxinwen/")
req = urllib.request.urlopen(url)
text = req.read()
html_text = text.decode("utf-8")
page = etree.HTML(html_text)
article['originalContent'] = encode(page.xpath("//div[contains(@class, 'TRS_Editor')]//p"))
if len(article['originalContent']) < 10:
continue
CONTENT_ENG = ''
for element in article['originalContent'].split("。"):
CONTENT_ENG += translate(element) + ' '
article['content'] = CONTENT_ENG
article['site'] = "Ministry of Finance"
article['originalSite'] = "财政部"
article['originalTitle'] = page.xpath("//meta[@name = 'ArticleTitle']/@content")[0]
article['title'] = translate(article['originalTitle'])
article['url'] = url
article['category']= "Financial News"
article['publishDate'] = datemodifier(page.xpath("//meta[@name = 'PubDate']/@content")[0],"%Y-%m-%d %H:%M:%S")
article['id'] = uuid.uuid5(uuid.NAMESPACE_OID, article['title']+article['publishDate'])
article['sentimentScore'], article['sentimentLabel'] = sentiment_computation(article['content'])
upsert_content(article)
except Exception as error:
print(error)
i = 0
while i > -1:
if i == 0:
CATEGORY_URL = "https://www.mof.gov.cn/zhengwuxinxi/zhengcejiedu/"
else:
CATEGORY_URL = f"https://www.mof.gov.cn/zhengwuxinxi/zhengcejiedu/index_{i}.htm"
i = i + 1
req = urllib.request.urlopen(CATEGORY_URL)
text = req.read()
html_text = text.decode("utf-8")
page = etree.HTML(html_text)
articlelist = page.xpath("//div[contains(@class, 'xwfb_listerji')]/ul/li[not(@class = 'clear')]")
for article in articlelist:
if isinstance(article, etree._Element):
subelement = etree.tostring(article).decode()
subpage = etree.HTML(subelement)
date = subpage.xpath("//span/text()")[0]
parsed_datetime = datetime.strptime(time.strftime("%Y-%m-%d", time.strptime(date,"%Y-%m-%d")), "%Y-%m-%d")
if parsed_datetime < (datetime.today() - timedelta(days=183)):
i = -1
else:
urls = subpage.xpath("//a[contains(@target, '_blank')]/@href")
for url in urls:
try:
article = {}
url = url.replace("./", CATEGORY_URL)
req = urllib.request.urlopen(url)
text = req.read()
html_text = text.decode("utf-8")
page = etree.HTML(html_text)
article['originalContent'] = encode(page.xpath("//div[contains(@class, 'TRS_Editor')]//p"))
if len(article['originalContent']) < 10:
continue
CONTENT_ENG = ''
for element in article['originalContent'].split("。"):
CONTENT_ENG += translate(element) + ' '
article['content'] = CONTENT_ENG
article['site'] = "Ministry of Finance"
article['originalSite'] = "财政部"
article['originalTitle'] = page.xpath("//meta[@name = 'ArticleTitle']/@content")[0]
article['title'] = translate(article['originalTitle'])
article['url'] = url
article['category']= "Policy Interpretation"
article['publishDate'] = datemodifier(page.xpath("//meta[@name = 'PubDate']/@content")[0], "%Y-%m-%d %H:%M:%S")
article['id'] = uuid.uuid5(uuid.NAMESPACE_OID, article['title']+article['publishDate'])
article['sentimentScore'], article['sentimentLabel'] = sentiment_computation(article['content'])
upsert_content(article)
except Exception as error:
print(error)
# categoryu_urls = ["https://www.mof.gov.cn/zhengwuxinxi/zhengcefabu/"]
# for categoryu_url in categoryu_urls:
# req = urllib.request.urlopen(categoryu_url)
# text = req.read()
# html_text = text.decode("utf-8")
# page = etree.HTML(html_text)
# articlelist = page.xpath("//div[contains(@class, 'xwfb_listerji')]/ul/li[not(@class = 'clear')]")
# for article in articlelist:
# if isinstance(article, etree._Element):
# subelement = etree.tostring(article).decode()
# subpage = etree.HTML(subelement)
# date = subpage.xpath("//span/text()")[0]
# parsed_datetime = datetime.strptime(time.strftime("%Y-%m-%d", time.strptime(date,"%Y-%m-%d")), "%Y-%m-%d")
# if parsed_datetime > (datetime.today() - timedelta(days=183)):
# urls = subpage.xpath("//a[contains(@target, '_blank')]/@href")
# for url in urls:
# try:
# article = {}
# url = url.replace("./", categoryu_url)
# req = urllib.request.urlopen(url)
# text = req.read()
# html_text = text.decode("utf-8")
# page = etree.HTML(html_text)
# article['originalContent'] = encode(page.xpath("//div[contains(@class, 'TRS_Editor')]//p"))
# content_eng = ''
# for element in article['originalContent'].split("。"):
# content_eng += translator.translate(element, dest='en').text + ' '
# article['content'] = content_eng
# article['site'] = "Ministry of Finance"
# article['originalSite'] = "财政部"
# article['originalTitle'] = page.xpath("//meta[@name = 'ArticleTitle']/@content")[0]
# article['title'] = translator.translate(article['originalTitle'], dest='en').text
# article['url'] = url
# article['category']= "Policy Release"
# article['publishDate'] = datemodifier(page.xpath("//meta[@name = 'PubDate']/@content")[0])
# article['id'] = uuid.uuid5(uuid.NAMESPACE_OID, article['title']+article['publishDate'])
# label_dict = {
# "positive": "+",
# "negative": "-",
# "neutral": "0",
# }
# sentiment_score = 0
# maximum_value = 0
# raw_sentiment = analyzer(article['content'][:512], return_all_scores=True)
# sentiment_label = None
# for sentiment_dict in raw_sentiment[0]:
# value = sentiment_dict["score"]
# if value > maximum_value:
# sentiment_label = sentiment_dict["label"]
# maximum_value = value
# if sentiment_dict["label"] == "positive":
# sentiment_score = sentiment_score + value
# if sentiment_dict["label"] == "negative":
# sentiment_score = sentiment_score - value
# else:
# sentiment_score = sentiment_score + 0
# article['sentimentScore'] = sentiment_score
# article['sentimentLabel'] = label_dict[sentiment_label]
# upsert_content(article)
# except Exception as error:
# print(error)
|