import os, urllib2,urllib
from xml.dom.minidom import parse
要解析URL中的XMl文件,必须先把这个xml文件下载下来再解析,所以:
def downLoadURL(file_name,URL):
PATH=os.path.abspath('.')+r'\result'
dest_dir = os.path.join(PATH, file_name)
try:
urllib.urlretrieve(URL, dest_dir)
print 'saving xml in the:', dest_dir
except:
print '\tError retrieving the URL:', dest_dir
#file_name为下载的xml保存的文件名,URL为要解析的xml网址
dom = parse(dest_dir)
root = dom.documentElement
itemlist = root.getElementsByTagName('package')
urls=[]
username = '******' password = '******' values = {'Username': username, 'Password': password} #对于xml的链接中需要登录的网址,需要填入用户名和密码
for item in itemlist:
als = item.getAttribute("articleUrl")
urls.append(als)
url_all=tuple(set(urls)) #去除xml中重复的链接网址
for url_each in url_all: url_each = url_each.encode("utf-8") if url_each!="":
try:
data = urllib.urlencode(values)
req = urllib2.Request(url_each, data)
response = urllib2.urlopen(req,timeout=30)
if response.code == 200: #response.code=200则证明这个网址存在
print 'Exist!' except urllib2.HTTPError,e:
print url_each
print e.code,e.reason