一、python获取指定网页上所有超链接
python通过urllib2抓取网页,然后通过正则表达式分析网页上的全部url地址。
代码:
复制代码 代码示例:
#!/usr/bin/python
import urllib2
import re
#connect to a URL
website = urllib2.urlopen(url)
#read html code
html = website.read()
#use re.findall to get all the links
links = re.findall('"((http|ftp)s?://.*?)"', html)
print links
二、python实现网页链接提取
python如何提取网页链接?
例子:
复制代码 代码示例:
#encoding:utf-8
import socket
import htmllib,formatter
def open_socket(host,servname):
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
port=socket.getservbyname(servname)
s.connect((host,port))
return s
host=''
host=input('请输入网址n')
mysocket=open_socket(host,'http')
message='GET http://%s/nn'%(host,)
mysocket.send(message)
file=mysocket.makefile()
htmldata=file.read()
file.close()
parser=htmllib.HTMLParser(formatter.NullFormatter())
parser.feed(htmldata)
print 'n'.join(parser.anchorlist)
parser.close()