什么是url?
url是internet上描述信息资源的字符串,主要用在各种www客户程序和服务器程序上。采用url可以用一种统一的格式来描述各种信息资源,包括文件、服务器的地址和目录等。url的一般格式为(带方括号[]的为可选项):
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
url的格式由三部分组成:
①第一部分是协议(或称为服务方式)。
②第二部分是存有该资源的主机ip地址(有时也包括端口号)。
③第三部分是主机资源的具体地址,如目录和文件名等。
第一部分和第二部分用“://”符号隔开,
第二部分和第三部分用“/”符号隔开。
第一部分和第二部分是不可缺少的,第三部分有时可以省略。
python提取页面内url的方法
from bs4 import beautifulsoup
import time,re,urllib2
t=time.time()
websiteurls={}
def scanpage(url):
websiteurl=url
t=time.time()
n=0
html=urllib2.urlopen(websiteurl).read()
soup=beautifulsoup(html)
pageurls=[]
upageurls={}
pageurls=soup.find_all(“a”,href=true)
for links in pageurls:
if websiteurl in links.get(“href”) and links.get(“href”) not in upageurls and links.get(“href”) not in websiteurls:
upageurls[links.get(“href”)]=0
for links in upageurls.keys():
try:
urllib2.urlopen(links).getcode()
except:
print “connect failed”
else:
t2=time.time()
upageurls[links]=urllib2.urlopen(links).getcode()
print n,
print links,
print upageurls[links]
t1=time.time()
print t1-t2
n+=1
print (“total is “+repr(n)+” links”)
print time.time()-t
scanpage(“