首页 > 编程语言 > 详细

python抓取网站URL小工具

时间:2015-09-28 20:36:21      阅读:186      评论:0      收藏:0      [点我收藏+]

1、安装Python requests模块(通过pip):

技术分享

环境搭建好了!

2、测试一下抓取URL的过程:

技术分享

技术分享

抓取出来的URL有JavaScript代码,正则上还有待更加完善,有兴趣的可以研究下~!

工具源代码:

#coding:utf-8

import sys

import re

import requests

#获取输入URL,并获取网页text

input = raw_input("please input URL format like this(http://www.baidu.com):")

print ‘input : %s‘ % input

r = requests.get(input)

data = r.text

#利用正则查找所有URL

link_list =re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\‘).+?(?=\‘)" ,data)

count = 0

for url in link_list:

    file = open("c:\\test.txt", "a")

    file.write(url+"\n")

    count = count + 1

    print url

print ‘\n‘

print ‘total URL is:‘ + str(count)

print ‘\n‘

print ‘crawling achieve...‘

file.close()

python抓取网站URL小工具

原文:http://www.cnblogs.com/milantgh/p/4844712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!