python实现网页链接提取的方法分享

yipeiwu_com6年前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/\n\n'%(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()

相关文章

python基于property()函数定义属性

这篇文章主要介绍了python基于property()函数定义属性,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下正常情况下,类包含的属性...

在Python中使用判断语句和循环的教程

条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断。 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if a...

详解pyinstaller selenium python3 chrome打包问题

详解pyinstaller selenium python3 chrome打包问题

今天打包selenium一个简单的请求,打完包本机运行exe没有问题,换台机器就闪退,非常蛋疼找了半天原因。 下面简述下,防止踩坑,如果闪退十有八九是浏览器版本跟浏览器插件对不上。 首先...

python list多级排序知识点总结

在python3的sorted中去掉了cmp参数,转而推荐“key+lambda”的方式来排序。 如果需要对python的list进行多级排序。有如下的数据: list_num =...

Python列表推导式的使用方法

1.列表推导式书写形式:   [表达式 for 变量 in 列表]    或者  [表达式 for 变量 in 列表 if 条件] 2.举例说明:...