python提取頁面內(nèi)url列表的方法
更新時間:2015年05月25日 12:27:51 作者:小蘿莉
這篇文章主要介紹了python提取頁面內(nèi)url列表的方法,涉及Python操作頁面元素的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了python提取頁面內(nèi)url列表的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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("http://news.163.com/")
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Python實現(xiàn)前端樣式尺寸單位轉(zhuǎn)換
在?Web?前端項目開發(fā)時,樣式尺寸都是以?rpx?為單位,可是?UI?設(shè)計師在看完開發(fā)后的?UI?,卻要求都以?px?為單位,所以本文就和大家分享一個利用Python就能實現(xiàn)尺寸單位轉(zhuǎn)換的方法吧2023-06-06
pytorch-gpu安裝的經(jīng)驗與教訓(xùn)
本文主要介紹了pytorch-gpu安裝的經(jīng)驗與教訓(xùn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-01-01
Python如何把不同類型數(shù)據(jù)的json序列化
這篇文章主要介紹了Python如何把不同類型數(shù)據(jù)的json序列化,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04

