提交 0be1bdd9 作者: 薛凌堃

广西壮族自治区人民政府

上级 e7e9818f
import json import json
...@@ -6,10 +6,17 @@ import numpy as np ...@@ -6,10 +6,17 @@ import numpy as np
import pandas as pd import pandas as pd
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from base import BaseCore import BaseCore
baseCore = BaseCore.BaseCore() baseCore = BaseCore.BaseCore()
log = baseCore.getLogger() log = baseCore.getLogger()
from reits import Policy
policy = Policy()
topic = 'policy'
webname = '广西壮族自治区人民政府'
headers = { headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -60,11 +67,9 @@ def getDataJson(page): ...@@ -60,11 +67,9 @@ def getDataJson(page):
def getContent(url, publishDate, num): def getContent(url, publishDate, num):
fjhref_list = '' id_list = []
fjtitle_list = ''
url_ = url.split('/')[-1]
url_ = url.replace(url_, '')
soup = getSoup(url) soup = getSoup(url)
policy.paserUrl(soup, url)
contentWithTag = soup.find('div', attrs={'id': 'articleFile'}) contentWithTag = soup.find('div', attrs={'id': 'articleFile'})
try: try:
scripts = contentWithTag.find_all('script') scripts = contentWithTag.find_all('script')
...@@ -79,46 +84,21 @@ def getContent(url, publishDate, num): ...@@ -79,46 +84,21 @@ def getContent(url, publishDate, num):
except: except:
pass pass
content = contentWithTag.text content = contentWithTag.text
img_list = contentWithTag.find_all('img')
num_ = 1
for img in img_list:
fj_href = img.get('src')
fjhref_list += fj_href + '\n'
if 'http' not in fj_href:
fj_href = url_ + fj_href
fj_title = img.get('title')
if fj_title == '':
fj_title = str(num_)
category = os.path.splitext(fj_href)[1]
if category not in fj_title:
fj_title = fj_title + category
fj_title = f'{num}-{publishDate[:10]}-{fj_title}'
fjtitle_list += fj_title + '\n'
fjcontent = getFjContent(fj_href)
file = f'./相关政策/广西壮族自治区人民政府/政策文件/{fj_title}'
with open(file, 'wb') as f:
f.write(fjcontent)
log.info(f'{fj_title}===附件下载成功')
a_list = soup.find('div', class_='downloadfile').find_all('a') a_list = soup.find('div', class_='downloadfile').find_all('a')
for a in a_list: for a in a_list:
fj_href = a.get('href') fj_href = a.get('href')
if 'http' not in fj_href:
fj_href = url_ + fj_href
fjhref_list += fj_href + '\n'
fj_title = a.text.lstrip().strip() fj_title = a.text.lstrip().strip()
category = os.path.splitext(fj_href)[1] category = os.path.splitext(fj_href)[1]
if category not in fj_title: if category not in fj_title:
fj_title = fj_title + category fj_title = fj_title + category
fj_title = f'{num}-{publishDate[:10]}-{fj_title}'
fjtitle_list += fj_title + '\n' att_id, full_path = policy.attuributefile(fj_title, fj_href, num, publishDate)
fjcontent = getFjContent(fj_href) if att_id:
file = f'./相关政策/广西壮族自治区人民政府/政策文件/{fj_title}' id_list.append(att_id)
with open(file, 'wb') as f: a['href'] = full_path
f.write(fjcontent)
log.info(f'{fj_title}===附件下载成功') return content, contentWithTag, id_list
fjhref_list = fjhref_list.lstrip().strip()
fjtitle_list = fjtitle_list.lstrip().strip()
return content,fjtitle_list,fjhref_list
def getData(data_, num): def getData(data_, num):
...@@ -126,20 +106,47 @@ def getData(data_, num): ...@@ -126,20 +106,47 @@ def getData(data_, num):
publishDate = data_['data']['time'] publishDate = data_['data']['time']
origin = '广西壮族自治区人民政府' origin = '广西壮族自治区人民政府'
href = data_['data']['url'] href = data_['data']['url']
# 根据链接判重
is_member = baseCore.r.sismember('REITs::' + webname, href)
if is_member:
return
writtenDate = data_['data']['table-10'] writtenDate = data_['data']['table-10']
organ = data_['data']['source'] organ = data_['data']['source']
pub_hao = data_['data']['table-5'] pub_hao = data_['data']['table-5']
summary = data_['data']['table-7'] summary = data_['data']['table-7']
content, fjtitle_list, fjhref_list = getContent(href, publishDate, num) content, contentWithTag, id_list = getContent(href, publishDate, num)
data = [num, title, publishDate, origin, href, writtenDate, organ, pub_hao, summary, content, fjtitle_list, contentWithTag_str = str(contentWithTag)
fjhref_list] time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return data 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': '1729045187128119298',
}
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(): def doJob():
if not os.path.exists('./相关政策/广西壮族自治区人民政府/政策文件'):
os.makedirs('./相关政策/广西壮族自治区人民政府/政策文件')
data_list = []
num = 1 num = 1
total = getTotal() total = getTotal()
for page in range(1, total + 1): for page in range(1, total + 1):
...@@ -149,14 +156,11 @@ def doJob(): ...@@ -149,14 +156,11 @@ def doJob():
title = data_['data']['title'] title = data_['data']['title']
if title not in title_list: if title not in title_list:
title_list.append(title) title_list.append(title)
data = getData(data_, num) getData(data_, num)
data_list.append(data)
log.info(f'{title}===采集成功')
num += 1 num += 1
time.sleep(2) time.sleep(2)
df = pd.DataFrame(np.array(data_list))
df.columns = ['序号', '标题', '发布时间', '来源', '原文链接', '发文时间', '发文机构', '发文字号', '摘要', '正文', '附件名称', '附件连接']
df.to_excel('./相关政策/江西省人民政府/广西壮族自治区人民政府政策文件.xlsx', index=False)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论