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遍历一个目录,输出所有的文件名的实例

python 获取一个文件夹内(包括子文件夹)所有文件的名字和路径 import os dir = "e:\\" for root, dirs, files in os.walk(d...

python opencv3实现人脸识别(windows)

本文实例为大家分享了python人脸识别程序,大家可进行测试 #coding:utf-8 import cv2 import sys from PIL import Ima...

tornado框架blog模块分析与使用

复制代码 代码如下:#!/usr/bin/env python## Copyright 2009 Facebook## Licensed under the Apache License...

详解将Python程序(.py)转换为Windows可执行文件(.exe)

详解将Python程序(.py)转换为Windows可执行文件(.exe)

python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! /post/165763.htm),并要将软件打包为可...

基于Pandas读取csv文件Error的总结

OSError:报错1 <span style="font-size:14px;">pandas\_libs\parsers.pyx in pandas._libs.pa...