ディレクトリタイトル
説明する
まず、notepad++ を使用して、ANSI エンコードされた Bat ファイルを書き込み、破損したファイルのダウンロード ディレクトリを作成します。Python を使用して
PDF をダウンロードすると、ネットワークやその他の環境によって簡単に妨害され、ファイルが破損します。
検出コード
import os
import re
import time
import math
import openpyxl
import fitz
row=0
order=0
mywb = openpyxl.Workbook()
mysheet = mywb.active
input=r'G:\年报'
for i in range(0,6):
path=input+'\\'+str(2015+i)+'\\'+"制造"
os.chdir(path)
for file in os.listdir():
if os.path.splitext(file)[1]=='.pdf':
order+=1
print(str(order)+'、'+file+':processing......')
code=os.path.splitext(file)[0][:6]
year=2015+i
try:
doc = fitz.open(file)
except:
row += 1
values = [code, year,1]
for j in range(0,3):
mysheet.cell(row=row, column=j+1, value=values[j])
print(str(order) + '、' + file + ' is wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.')
else:
print(str(order) + '、' + file + ' is ok.')
path = input + '\\' + str(2015 + i) + '\\' + "其他"
os.chdir(path)
for file in os.listdir():
if os.path.splitext(file)[1]=='.pdf':
order += 1
print(str(order)+'、'+file+':processing......')
code = os.path.splitext(file)[0][:6]
year = 2015 + i
try:
doc = fitz.open(file)
except:
row += 1
values = [code, year, 0]
for j in range(0, 3):
mysheet.cell(row=row, column=j + 1, value=values[j])
print(str(order) + '、' + file + ' is wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.')
else:
print(str(order) + '、' + file + ' is ok.')
mywb.save(r'G:\pdf损坏统计.xlsx')
コードを再ダウンロードする
from fake_useragent import UserAgent
import os
import re
import openpyxl
import random
import requests
import urllib.request
import time
def download(url, downpath,filename,i,code):
if filename in os.listdir(downpath):
print(str(i)+"th "+code+" already there")
return
file_path=downpath+'/'+filename
ua = UserAgent()
user_agent = {
"User-Agent":ua.random}
proxies_list = [
{
"http": "220.168.52.245"}
, {
"http": "202.55.5.209"}
, {
"http": "106.54.128.253"}
, {
"http": "47.106.105.236"}
]
request = urllib.request.Request(url, headers=user_agent)
#response = urllib.request.urlopen(request)
proxy=random.choice(proxies_list)
print(proxy)
t1=time.monotonic()
#response = requests.get(url, headers=user_agent, proxies=proxy)
response = requests.get(url, headers=user_agent)
t2 =time.monotonic()
f = open(file_path, 'wb')
f.write(response.content)
#response对象数据存储
f.close()
print(str(i)+'th '+code+' is done'+" ",str(round(t2-t1,3))+'s')
def code_revise(code_cell):
code=(code_cell.value)
code=str(code)#其实不是数字但不会报错
#用value就是数值,text不能用
for i in range(1,6-len(code)+1):
code='0'+code
return code
def url_revise(url):
#普通命令str.replace(old, new[, max])
#old --将被替换的子字符串。.new --新字符串,用于替换old子字符串。max --可选字符串,替换不超过max次
#re.sub(pattern, repl, string, count=0, flags=0)
#参数含义依次为旧字符正则匹配式、新子串、原文、次数默认全部替换
#print("url1:" + url)
old1=re.compile(r'disclosure/detail\?stockCode=\d+&announcementId')
old2=re.compile(r'orgId=\w+\d+&announcementTime')
new1='announcement/download?bulletinId'
new2='announceTime'
url=re.sub(old1,new1,url)
url = re.sub(old2, new2, url)
#print("url2:"+url)
return url
def pdf_revise(code,year,flag,order):
path=str(year)+'/'+ind[flag]+'/'+str(year)+'-'+str(year+1)+ind2[flag]+'.xlsx'
sheet=sheetlist[path]
for row in sheet.rows:
if code_revise(row[0])==code:
filename = code + '_' + row[2].value + '.pdf'
url = url_revise(row[4].value)
downpath=path=str(year)+'/'+ind[flag]
download(url, downpath, filename, order, code)
return
print(str(order) + 'th ' + code + ' not found')
wb = openpyxl.load_workbook(r'G:\pdf损坏统计.xlsx')
wbsheet=wb.active
ind=['其他','制造']
ind2=['其他行业','制造业']
os.chdir(r'G:\年报修复')
wblist={
}
sheetlist={
}
for i in range(0,6):
for j in range(0,2):
path=str(2015+i)+'/'+ind[j]+'/'+str(2015+i)+'-'+str(2016+i)+ind2[j]+'.xlsx'
wbtmp=openpyxl.load_workbook(path)
wblist[path]=(wbtmp)
sheetlist[path]=(wbtmp.active)
order=0
for row in wbsheet.rows:
if row[0].value == None:
# print("row[0]:",row[0].value)
break
else:
order+=1
code=row[0].value
year=row[1].value
flag=row[2].value
pdf_revise(code,year,flag,order)