python获取网页上所有链接,并保存到sqlite3数据表中,并用浏览器打开。如果该表已存在,则直接从表中读取链接并打开。
表名中去掉开头"http://", 结尾"/", 端口号,替换中间字符".", "/"为"_"
用到的python库:
sgmllib,urllib网页有关
re正则表达式
sqlite3数据表
subprocess子进程
#!/usr/bin/env python
#-*-coding:utf-8 -*-
from sgmllib import SGMLParser
import urllib,re
import sys, os, string, time
import sqlite3
import subprocess, signal
class UrlList(SGMLParser):
def reset(self):
self.urls=[]
SGMLParser.reset(self)
def start_a(self,attrs):
href=[v for k,v in attrs if k=='href']
if href:
self.urls.extend(href)
def get_urls(url):
try:
usock=urllib.urlopen(url)
except:
print "