提交 78a94cdb 作者: 薛凌堃

上海市人民政府

上级 69ba202a
import json
import json
......@@ -8,10 +8,17 @@ import requests
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from base import BaseCore
import BaseCore
baseCore = BaseCore.BaseCore()
log = baseCore.getLogger()
from reits import Policy
policy = Policy()
topic = 'policy'
webname = '上海市人民政府'
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
......@@ -54,17 +61,21 @@ def getFjContent(url):
def getData(data_, driver, num):
fjhref_list = ''
fjtitle_list = ''
id_list = []
title = data_['title']['raw']
publishDate = data_['date']['raw']
origin = '上海市人民政府'
href = data_['url']['raw']
# 根据链接判重
is_member = baseCore.r.sismember('REITs::' + webname, href)
if is_member:
return
organ = data_['fwjg']['raw']
pub_hao = data_['wh']['raw']
summary = data_['content']['snippet']
driver.get(href)
time.sleep(1)
contentWithTag = driver.find_element(By.CLASS_NAME, 'Article_content')
content = driver.find_element(By.CLASS_NAME, 'Article_content').text.lstrip().strip()
timeTag = driver.find_element(By.CLASS_NAME, 'PBtime').text
try:
......@@ -76,47 +87,71 @@ def getData(data_, driver, num):
except:
writtenDate = timeTag.split('印发日期:')[1].lstrip().strip()
except:
writtenDate = ''
writtenDate = None
try:
a_list = driver.find_element(By.CLASS_NAME, 'gaoj-list').find_elements(By.TAG_NAME, 'a')
for a in a_list:
fj_href = a.get_attribute('href')
fjhref_list += fj_href + '\n'
category = os.path.splitext(href)[1]
fj_title = f'{num}-{publishDate}-{a.text.lstrip().strip()}'
category = os.path.splitext(fj_href)[1]
fj_title = a.text.lstrip().strip()
if '<' in fj_title or '>' in fj_title:
fj_title = fj_title.replace('<', '').replace('>', '')
if category not in fj_title:
fj_title = fj_title + category
fjtitle_list += fj_title + '\n'
# fjcontent = getFjContent(href)
# file = f'./相关政策/内蒙古自治区人民政府/政策文件/{fj_title}'
# with open(file, 'wb') as f:
# f.write(fjcontent)
# log.info(f'{fj_title}===附件下载成功')
att_id, full_path = policy.attuributefile(fj_title, fj_href, num, publishDate)
if att_id:
id_list.append(att_id)
a['href'] = full_path
else:
pass
except:
pass
data = [num, title, publishDate, origin, href, writtenDate, organ, pub_hao, summary, content, fjtitle_list,
fjhref_list]
return data
# data = [num, title, publishDate, origin, href, writtenDate, organ, pub_hao, summary, content, fjtitle_list,
# fjhref_list]
contentWithTag_str = str(contentWithTag)
time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
dic_info = {
'attachmentIds': id_list,
'author': '',
'content': content,
'contentWithTag': contentWithTag_str,
'deleteFlag': 0,
'id': '',
'title': title,
'publishDate': publishDate,
'origin': origin,
'sourceAddress': href,
'writtenDate': writtenDate,
'organ': organ,
'topicClassification': '',
'issuedNumber': pub_hao,
'summary': summary,
'createDate': time_now,
'sid': '1729042751554506754',
}
try:
baseCore.sendkafka(dic_info, topic)
baseCore.r.sadd('REITs::' + webname, href)
log.info(f'采集成功--{title}--{href}')
except Exception as e:
for att_id in id_list:
baseCore.deliteATT(att_id)
return
def doJob():
if not os.path.exists('./相关政策/上海市人民政府/政策文件'):
os.makedirs('./相关政策/上海市人民政府/政策文件')
driver = baseCore.buildDriver()
data_list = []
# driver = baseCore.buildDriver()
driver = policy.createDriver()
num = 1
data_json = getDataJson()
for data_ in data_json:
data = getData(data_, driver, num)
log.info(f'{data[1]}===采集成功')
data_list.append(data)
# log.info(f'{data[1]}===采集成功')
# data_list.append(data)
num += 1
df = pd.DataFrame(np.array(data_list))
df.columns = ['序号', '标题', '发布时间', '来源', '原文链接', '发文时间', '发文机构', '发文字号', '摘要', '正文', '附件名称', '附件连接']
df.to_excel('./相关政策/上海市人民政府/上海市人民政府政策文件.xlsx', index=False)
if __name__ == '__main__':
doJob()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论