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

yipeiwu_com5年前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基于TCP实现会聊天的小机器人功能示例

Python基于TCP实现会聊天的小机器人功能示例

本文实例讲述了Python基于TCP实现会聊天的小机器人功能。分享给大家供大家参考,具体如下: 一 代码 1、服务端程序 import socket words ={'how are...

python中redis查看剩余过期时间及用正则通配符批量删除key的方法

具体代码如下所示: # -*- coding: utf-8 -*- import redis import datetime ''' # 1. redis设置过期时间的两种方式 e...

python的pdb调试命令的命令整理及实例

python的pdb调试命令的命令整理及实例 一、命令整理 pdb调试命令 完整命令 简写命令 描述...

Pyinstaller将py打包成exe的实例

Pyinstaller将py打包成exe的实例

背景:分享python编写的小脚本时,拷贝代码还缺各种环境,使用Pyinstaller将py可以打包成exe,直接运行即可 1、安装pyinstaller运行时所需要的windows拓展...

Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并

1. 简介 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。最主要的数据结...