python提取页面内url列表的方法

yipeiwu_com6年前Python基础

本文实例讲述了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("http://news.163.com/")

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python异常处理例题整理

Python异常处理例题整理

什么是异常? 异常是Python对象,表示一个错误。当Python脚本发生异常时我们需要捕获处理它,否则程序会终止执行。在程序运行过程中,总会遇到各种各样的错误,有的错误是程序编写有问题...

OpenCV-Python 摄像头实时检测人脸代码实例

OpenCV-Python 摄像头实时检测人脸代码实例

参考 OpenCV摄像头使用 代码 import cv2 cap = cv2.VideoCapture(4) # 使用第5个摄像头(我的电脑插了5个摄像头) face_cascad...

python仿evething的文件搜索器实例代码

python仿evething的文件搜索器实例代码

今天看到everything搜索速度秒杀windows自带的文件管理器,所以特地模仿everything实现了文件搜索以及打开对应文件的功能,首先来一张搜索对比图。 这是evething...

Python图像处理之图片文字识别功能(OCR)

Python图像处理之图片文字识别功能(OCR)

OCR与Tesseract介绍 将图片翻译成文字一般被称为光学文字识别(Optical Character Recognition,OCR)。可以实现OCR 的底层库并不多,目前很多库都...

Python实现两个list对应元素相减操作示例

本文实例讲述了Python实现两个list对应元素相减操作。分享给大家供大家参考,具体如下: 两个list的对应元素操作,这里以相减为例: # coding=gbk v1 = [21...