前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >0x6 Python教程:Spidering

0x6 Python教程:Spidering

作者头像
重生信息安全
发布2020-04-26 12:46:52
7460
发布2020-04-26 12:46:52
举报
文章被收录于专栏:重生信息安全

Spidering:

这篇Python教程将介绍一些新模块(optparse,spider)来完成抓取Web应用程序的任务。通过跟踪Web应用程序中的链接来帮助构建站点地图,Web应用程序的嵌入是Web应用程序上的枚举链接内容的过程。抓住Web应用程序是利用Python创建快速脚本的一个很好的用例。

您可以通过解析请求响应上的href标记来创建爬网程序脚本,然后创建其他请求。您还可以利用名为“Spider”的Python模块在更少的代码行中执行此操作:

您可以配置几个与蜘蛛如何工作相关的选项“myspider(b = URL.strip(),w = 200,d = 5,t = 5)”。此函数将返回两个子URL和路径列表。您可以通过更改传递给myspider函数的参数来修改蜘蛛的工作方式:

b - 基本Web URL(默认值:无) w - 要爬网的资源量(默认值:200) d - 要爬网的层次结构深度(默认值:5) ) t - 线程数(默认值:无)

这篇博文简要介绍了如何通过利用Python来与Web资源进行交互。存在许多用于编写Web资源交互脚本的高级用例。未来的博客文章将通过针对Web服务器的脚本攻击来演示一些更高级的用例。

利用Python蜘蛛模块的代码片段:

代码语言:javascript
复制
#!/usr/bin/pythonfrom spider import webspider as myspiderimport sys, optparse def crawler(URLs):        for line in open(URLs, 'r'):                URL = line.strip()                links = myspider(b=URL.strip(), w=200, d=5, t=5)                link_count = len(links[0])                out = URL+": has a link count of "+str(link_count)                print "[+] Web Crawl Results for: "+URL                print out                for item in links[1]:                        print item def main():# This optparse module allows you to build command line switches in your scripts# This will set the usage to '-r' and have it stored to a variable URLs# Then we will open the file given at the command line with -r and attempt to spider        parser = optparse.OptionParser(sys.argv[0]+' '+        '-r <file_with URLs>')        parser.add_option('-r', dest='URLs', type='string',                 help='specify target file with URLs')        (options, args) = parser.parse_args()        URLs=options.URLs         if (URLs == None):                print parser.usage                sys.exit(0)        else:                crawler(URLs) if __name__ == "__main__":      main()
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 重生信息安全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档