Python3.6 爬虫初体验--urllib、beautifulsoup(一)
来源:互联网 发布:边缘融合软件 破解 编辑:程序博客网 时间:2024/06/06 05:28
经常查询IP地址相关,有时目标太多需要批量操作,于是想到python爬虫,批量查询、模式输出,废话不多说,代码奉上!
p.s. 涉及到的两个主要模块–urllib、BeautifulSoup
p.s. 本文主要介绍以上两模块使用,故涉及到的IP查询网站就用www.URL.com代替了
import urllib.request #python3.6中url请求模块只能用这种方式导入,其他方式亲测失败。。。from bs4 import BeautifulSoup #同上,python3.6中的beautifulsoup导入方式,其他方式亲测失败。。。import reimport timewith open('ipresult.txt','w') as out: #创建并打开结果存储文本 with open('ipurl.txt','r') as list: #打开已经存在的需查询域名文本 for data in list: #以下为打印出所查询域名以及分割线 print('-'*50,file=out) print("网站域名",data,file=out) url="http://www.URL.com/" #查询请求网址 full_url=url+data #构造每个域名的查询url data=urllib.request.urlopen(full_url).read() #发起请求并读取回应 data=data.decode('UTF-8') soup = BeautifulSoup(data,"html.parser") #使用beautifulsoup分析回应 #time.sleep(20)#以下为抓取回的页面中涉及ip地址及物理位置的html内容,根据其中的标签及class提取出想要的内容 """ <p class="WhwtdWrap bg-blue08 col-gray03"> <span class="Whwtdhalf w15-0">域名/IP</span> <span class="Whwtdhalf w15-0">获取的IP地址</span> <span class="Whwtdhalf w15-0">数字地址</span> <span class="Whwtdhalf w50-0">IP的物理位置</span> </p> <p class="WhwtdWrap bor-b1s col-gray03"> <span class="Whwtdhalf w15-0">www.hXXXXX.com</span> <span class="Whwtdhalf w15-0">45.120.XX.XX</span> <span class="Whwtdhalf w15-0">7628XXXXXX</span> <span class="Whwtdhalf w50-0">IANA 保留地址</span> </p>""" for link in soup.find_all('span'): #此for循环为通过匹配html标签“span”,大体定位ip地址 text_span = link.get_text() ip_finder = re.compile(r'(\d{1,3}\.){3}\d{1,3}') #此处正则匹配ip地址 ip = ip_finder.search(text_span) if ip : print("IP地址为:",ip.group(),file=out) #正则导出ip地址内容 lo_finder = link.get('class') #使用lo_finder代表从span标签中提取出全部class标签 lo_class = ['Whwtdhalf', 'w50-0'] #观察以上html发现标记物理位置的class为class="Whwtdhalf w50-0",与其他class明显不同,故作if判断进一步提取,但是此时还有(IP的物理位置)这一文本内容也是此class if lo_finder == lo_class: lo_text = link.get_text() #print(lo_text) if lo_text != 'IP的物理位置': #排除相同class标签的另一文本内容(IP的物理位置),即可提取出想要的物理位置 print("服务器位于",lo_text,file=out) #lo_patten = re.compile(r'') #lo = lo_patten.match(lo_text) #if lo : # print("服务器位于",lo.group(),file=out) #print(lo,file=out) print('-'*50,file=out)print("运行结束")
输出结果示例如下(为保护隐私有关信息用”X”代替):
--------------------------------------------------网站域名 www.hXXXXX.comIP地址为: 103.56.XX.XX服务器位于 亚太地区 --------------------------------------------------网站域名 www.hXXXXX.comIP地址为: 45.127.XX.XX服务器位于 IANA 保留地址--------------------------------------------------网站域名 www.hXXXXXX.comIP地址为: 124.248.XX.XX服务器位于 香港 XXX(SunnXXXXXX)数据中心(XXXXXXX)--------------------------------------------------网站域名 www.hXXXXXXX.comIP地址为: 42.123.XX.XX服务器位于 贵州省贵阳市 --------------------------------------------------网站域名 www.hXXXXXX.comIP地址为: 119.28.XX.XX服务器位于 香港 北京XXXXXXX有限责任公司
阅读全文
0 0
- Python3.6 爬虫初体验--urllib、beautifulsoup(一)
- python3爬虫初探(一)之urllib.request
- [爬虫] Python爬虫 urllib BeautifulSoup
- Python爬虫基础细节(urllib+cookielib+BeautifulSoup)
- python爬虫01:urllib初体验
- [Python3.x]网络爬虫(一):利用urllib通过指定的URL抓取网页内容
- Python3 爬虫学习(一):urllib库的使用及简单的爬取
- python3网络爬虫一《使用urllib.request发送请求》
- Python3学习笔记12--urllib,BeautifulSoup
- urllib/urllib2和BeautifulSoup爬虫学习
- Python3 urllib库爬虫 基础
- python3 urllib 爬虫乱码问题解决
- python3 urllib爬虫抓取记录
- 【Python3.6爬虫学习记录】(六)urllib详细使用方法(header,代理,超时,认证,异常处理)
- python3爬虫攻略(1):urllib.request使用(1)
- python3爬虫攻略(2):urllib.request(2)
- Python3爬虫学习笔记(1.urllib库详解)
- 【Python3.6爬虫学习记录】(二)使用BeautifulSoup爬取简单静态网页文章
- 软件测试
- Springboot基础篇 Controller
- GSON案例[L]
- php.ini 配置调优 —— 让 PHP 应用性能维持在更高水平
- 欢迎使用CSDN-markdown编辑器
- Python3.6 爬虫初体验--urllib、beautifulsoup(一)
- SVN版本冲突解决详解
- 用Promise优化nodejs的callback hell
- js调用exe文件 兼容所有浏览器
- 排序
- Zookeeper 命令行
- 图的异或 dfs树+线性基
- 见微知著----POJ2051(最小堆)
- 菜鸟末端轨迹(解密支撑每天251亿个包裹的数据库)