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

作者:小萝莉 时间:2022-12-26 15:29:27 

本文实例讲述了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,url
0
投稿

猜你喜欢

  • 提高ASP性能的最佳选择第二部分

    2007-08-15 12:46:00
  • Django静态文件加载失败解决方案

    2021-05-25 19:40:47
  • python使用selenium打开chrome浏览器时带用户登录信息实现过程详解

    2023-07-19 05:00:37
  • pandas行和列的获取的实现

    2022-06-05 07:57:49
  • Python使用中文正则表达式匹配指定中文字符串的方法示例

    2021-11-28 22:17:00
  • MATLAB数学建模之画图汇总

    2023-06-14 06:49:50
  • JavaScript实现自动变换表格边框颜色

    2024-06-07 15:25:53
  • SQL Server中使用判断语句(IF ELSE/CASE WHEN )案例

    2024-01-18 22:04:53
  • 如何做一个文本搜索?

    2010-07-12 19:00:00
  • python+pygame实现坦克大战

    2023-12-04 10:52:38
  • Mysql更换MyISAM存储引擎为Innodb的操作记录总结

    2024-01-22 05:54:25
  • python区块链创建多个交易教程

    2021-05-28 13:40:42
  • python实现一个围棋小游戏

    2022-02-02 18:52:50
  • sql server中datetime字段去除时间的语句

    2024-01-23 17:47:57
  • python新手练习实例之万年历

    2021-01-29 02:20:35
  • vuex中数据持久化插件vuex-persistedstate使用详解

    2024-04-26 17:41:54
  • SQL Server中的排名函数与分析函数详解

    2024-01-17 03:28:54
  • Docker安装MySQL8.0的实现方法

    2024-01-23 06:52:06
  • 奇妙的Javascript图片放大镜

    2024-04-30 08:51:22
  • Git在Windows中安装与使用教程

    2023-11-01 08:47:55
  • asp之家 网络编程 m.aspxhome.com