因为某些原因要收集一系列pdf,于是就想用搜索引擎自动搜索文件名,然后根据返回页面pdf文件的检查,如果有相应的pdf,那么就下载第一个pdf链接,并在同时用readme文件来记录相应的下载情况,因为只是临时用用,本着性价比的原则程序写得不是很完善。
import os
import urllib
import re
def cbk(a,b,c):
per = 100.0 * a * b / c
if per > 100:
per = 100
print '%.2f%%' % per
def setdir(dirname):
path="D:/down/newpdf/"+dirname
if not os.path.exists(path):
os.makedirs(path)
os.chdir(path)
os.getcwd()
def downpdf(url,file):
print url
filename,msg=urllib.urlretrieve(url,file,cbk)
def search(line):
url='http://cn.bing.com/search?q='+line
conn=urllib.urlopen(url)
nn=conn.read()
if len(nn)==292:
return False
else:
reg='<cite>(.*?)</cite>'
articles=re.compile(reg).findall(nn)
if len(articles)==0:
return False
else:
for url in articles:
if url[-4:]=='.pdf':