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程序设计有所帮助。

相关文章

tensorflow学习笔记之mnist的卷积神经网络实例

mnist的卷积神经网络例子和上一篇博文中的神经网络例子大部分是相同的。但是CNN层数要多一些,网络模型需要自己来构建。 程序比较复杂,我就分成几个部分来叙述。 首先,下载并加载数据:...

python利用requests库进行接口测试的方法详解

前言 之前介绍了接口测试中需要关注得测试点,现在我们来看看如何进行接口测试,现在接口测试工具有很多种,例如:postman,soapui,jemter等等,对于简单接口而言,或者我们只想...

使用Python来做一个屏幕录制工具的操作代码

使用Python来做一个屏幕录制工具的操作代码

一、写在前面 作为一名测试,有时候经常会遇到需要录屏记录自己操作,方便后续开发同学定位。以前都是用ScreenToGif来录屏制作成动态图,偶尔的机会看到python也能实现。那就赶紧...

Python GUI编程完整示例

Python GUI编程完整示例

本文实例讲述了Python GUI编程。分享给大家供大家参考,具体如下: import os from time import sleep from tkinter import *...

Python在不同目录下导入模块的实现方法

python在不同层级目录import模块的方法 使用python进行程序编写时,经常会调用不同目录下的模块及函数。本篇博客针对常见的模块调用讲解导入模块的方法。 1. 同级目录下的调用...