python实现自动抓取某站点内所有超链接

(仅供学习使用)

代码部分

#!/usr/bin/python

import requests
import time
import re
import sys, getopt #命令行选项
from bs4 import BeautifulSoup
localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #时间
z=[] #存取网站
x=[] #优化网站,去除冗杂部分 def main(argv):
url = '' #输入的网址
file_path = '' #保存路径
try:
opts, args = getopt.getopt(argv,"hu:f:",["url=","file="])
except getopt.GetoptError:
print ('allsite.py -u <url> -f <file>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h': #帮助
print ('allsite.py -u <url> -f <file>')
sys.exit()
elif opt in ("-u", "--url"): #输入网址
url = arg
re1 = requests.get(url) #get网站内容
re1.encoding = "utf-8"
html = re1.text
bt = BeautifulSoup(html, 'html.parser', )
hh = bt.find_all('a') #查找<a>元素
for site in hh:
z.append(site.get('href')) #进一步过滤得到超链接
for i in z:
if (re.match('//www', str(i)) or re.match('www', str(i))):
xx = str(i).replace('//www', 'www', 1)
x.append(xx)
elif (re.match('http', str(i))): #过滤
x.append(str(i))
elif (re.match('/', str(i))): #过滤
xx = str(i).replace("/", "", 1)
if (re.match('/', xx)):
xxx = str(xx).replace("/", "", 1)
x.append(xxx)
else:
x.append(url + xx)
else: #过滤
if (re.search('javascript', str(i)) == None):
x.append(url + str(i))
print(localtime + " 总共:" + str(len(x)) + "个网址") #输出超链接
for i in x:
print(i)
elif opt in ("-f", "--file"): #输入保存路径
file_path = arg
for i in x: #保存文件
with open(file_path, 'a') as file_object:
file_object.write(i)
file_object.write('\n') if __name__ == "__main__":
main(sys.argv[1:])

python超链接抓取工具的更多相关文章

  1. python 爬虫抓取心得

    quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quo ...

  2. Hawk-数据抓取工具

    Hawk-数据抓取工具:简明教程   Hawk: Advanced Crawler& ETL tool written in C#/WPF 1.软件介绍 HAWK是一种数据采集和清洗工具,依据 ...

  3. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  4. python数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

  5. python 处理抓取网页乱码

    python 处理抓取网页乱码问题一招鲜   相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找是否包含指定的信息. 在html = urllib2. ...

  6. Python爬虫----抓取豆瓣电影Top250

    有了上次利用python爬虫抓取糗事百科的经验,这次自己动手写了个爬虫抓取豆瓣电影Top250的简要信息. 1.观察url 首先观察一下网址的结构 http://movie.douban.com/to ...

  7. Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储

    Python爬虫可以说是好玩又好用了.现想利用Python爬取网页股票数据保存到本地csv数据文件中,同时想把股票数据保存到MySQL数据库中.需求有了,剩下的就是实现了. 在开始之前,保证已经安装好 ...

  8. Web站点抓取工具webhttrack

    近来发现Ubuntu下一个很好用的web站点抓取工具webhttrack,能够将给定网址的站点抓取到本地的目录中,并实现离线浏览,很实用. 1.安装webhttrack ubuntu 16.04的官方 ...

  9. python Web抓取(一)[没写完]

    需要的模块: python web抓取通过: webbrowser:是python自带的,打开浏览器获取指定页面 requests:从因特网上下载文件和网页 Beautiful Soup:解析HTML ...

随机推荐

  1. 第06组 Alpha冲刺(4/4)

    队名:福大帮 组长博客链接:https://www.cnblogs.com/mhq-mhq/p/11913386.html 作业博客 :https://edu.cnblogs.com/campus/f ...

  2. 第12组 Beta冲刺(5/5)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...

  3. BASE64使用场景

    BASE64使用场景 Base64就是一种基于64个可打印字符来表示二进制数据的方法. Base64编码是从二进制到字符的过程. 在项目中,将报文进行压缩.加密后,最后一步必然是使用base64编码, ...

  4. curl的速度为什么比file_get_contents快以及具体原因

    一.背景      大家做项目的时候,不免会看到前辈的代码.博主最近看到前辈有的时候请求外部接口用的是file_get_contents,有的用的是curl.稍微了解这两部分的同学都知道,curl在性 ...

  5. 升级ruby的版本 https://gems.ruby-china.com/

    升级ruby版本,有时候安装ruby的版本过低,需要进行升级,例如安装在centos6.7安装fpm需要ruby版本在1.9以上. 1.主机环境如下: 1 [root@test ~]# cat /et ...

  6. Python script to package the information of tracking benchmarks like LaSOT and GOT-10k into json files for Siamese network based trackers

    ############################################################################################ #### Fo ...

  7. Selenium自动化对非输入框的日历或日期控件的处理

    4.这个时候我们可以移除readonly的属性,问题就轻轻松松解决了,代码如下: String js = "document.getElementById('createTime').rem ...

  8. typescript - 8.命名空间

    基础 略. https://www.tslang.cn/docs/handbook/namespaces.html 多文件中的命名空间(一个文件分解为几个) 现在,我们把Validation命名空间分 ...

  9. k8s记录-yum本地仓库部署

    #1.安装插件yum install -y yum-plugin-downloadonly createrepo rsync #2.创建仓库目录mkdir -p /mirrors/centos#3.下 ...

  10. 【视频开发】 ffmpeg支持的硬解码接口

    To enable DXVA2, use the --enable-dxva2 ffmpeg configure switch. To test decoding, use the following ...