搜索
查看: 9427|回复: 3

Python爬虫 获取网站所有的网址

    [复制链接]

152

主题

99

回帖

540

积分

高级会员

中国黑客

积分
540
QQ
发表于 2020-2-21 18:06:17 | 显示全部楼层 |阅读模式 来自 甘肃兰州
本帖最后由 priess1314 于 2020-2-21 18:08 编辑

最近几天,闲来无事研究了Python爬虫。写了一个简单的脚本,来看看效果吧!



[pre]
import os  
import re  
import shutil  
  
REJECT_FILETYPE = 'rar,7z,css,js,jpg,jpeg,gif,bmp,png,swf,exe'
  
def getinfo(webaddress):   
    global REJECT_FILETYPE  
    url = 'http://'+webaddress+'/'  
    print 'Getting>>>>> '+url
    websitefilepath = os.path.abspath('.')+'/'+webaddress   
    if os.path.exists(websitefilepath):   
        shutil.rmtree(websitefilepath)     
    outputfilepath = os.path.abspath('.')+'/'+'output.txt'   
    fobj = open(outputfilepath,'w+')  
    command = 'wget -r -m -nv --reject='+REJECT_FILETYPE+' -o '+outputfilepath+' '+url  
    tmp0 = os.popen(command).readlines()  
    print >> fobj,tmp0  
    allinfo = fobj.read()
    target_url = re.compile(r'\".*?\"',re.DOTALL).findall(allinfo)  
    print  target_url
    target_num = len(target_url)  
    fobj1 = open('result.txt','w')     
    for i in range(target_num):
        if len(target_url[1:-1])<70:   
           print >> fobj1,target_url[1:-1]     
        else:  
          print "NO"
    fobj.close()  
    fobj1.close()  
    if os.path.exists(outputfilepath):   
        os.remove(outputfilepath)  
   
if __name__=="__main__":  
    webaddress = raw_input("Input the Website Address(without \"http:\")>")  
    getinfo(webaddress)  
    print "Well Done."
[/pre]

这里,我已逍遥子大表哥的博客为例。效果如下:
QQ截图20200221180802.png
谁有敬业福啊!
回复

使用道具 举报

2

主题

21

回帖

42

积分

vip用户

积分
42

活跃会员

发表于 2020-2-21 21:56:26 | 显示全部楼层
爬虫需谨慎,有个朋友因为提供爬虫技术爬了一个游戏网站,结果行政拘留7天,罚款5000

点评

说的是!  发表于 2020-2-22 14:24
回复 支持 反对

使用道具 举报

2

主题

4

回帖

10

积分

新手开车

积分
10
发表于 2020-2-23 11:50:38 | 显示全部楼层 来自 菲律宾
你好,有会渗透吗
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表