首頁  >  文章  >  後端開發  >  python 符合url中是否存在IP位址的方法

python 符合url中是否存在IP位址的方法

不言
不言原創
2018-06-04 11:11:001733瀏覽

這篇文章主要介紹了關於python 匹配url中是否存在IP位址的方法,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

因為需要檢測一個一個連結中是否包含了IP位址,在這裡需要使用到正規表示式,python完美的支援了正則表達式,在這裡使用re模組來完成,對正則表達式並不是很熟練,每次都是需要用的時候現查一下然後寫一下,這裡給出來自己的程式碼以及借鑒別人的匹配模式

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
功能:对于给定的URL,检测其中是否包含IP
'''
import re
def ip_exist_two(one_url):
	compile_rule = re.compile(r&#39;(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])&#39;)
	match_list = re.findall(compile_rule, one_url)
	if match_list:
		print match_list
	else:
		print &#39;missing................&#39;
def ip_exist_one(one_url):
	compile_rule = re.compile(r&#39;\d+[\.]\d+[\.]\d+[\.]\d+&#39;) 
	match_list = re.findall(compile_rule, one_url)
	if match_list:
		print match_list
	else:
		print &#39;missing................&#39;
if __name__ == &#39;__main__&#39;:
	ip_list = [&#39;http://101.23.45.67/sd/sd.html&#39;,&#39;http://www.baidu.com&#39;,
	&#39;http://34.54.65.3/dsdfjkk.htm&#39;,&#39;http://dhj.fdjjd.com/78078979/dsdfjkk.htm&#39;]
	for one_url in ip_list:
		ip_exist_one(one_url)
	print &#39;****************************************************&#39;
	for one_url in ip_list:
		ip_exist_two(one_url)

ip_exist_one(one_url)裡面是自己的匹配模式,個人感覺更賤練一下,ip_exist_two(one_url)裡面是網路上提供的匹配IP的正規表示式,感覺比較繁雜一下,不過試驗了一下都是可以正確匹配出來結果的。

下面是列印出來的結果

#
[&#39;101.23.45.67&#39;]
missing................
[&#39;34.54.65.3&#39;]
missing................
****************************************************
[&#39;101.23.45.67&#39;]
missing................
[&#39;34.54.65.3&#39;]
missing................

相關推薦:

Django專案中包含多個應用程式時對url的設定方法

以上是python 符合url中是否存在IP位址的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn