Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python 2.7从源代码中获取IP_Python_Html_Parsing_Ip - Fatal编程技术网

python 2.7从源代码中获取IP

python 2.7从源代码中获取IP,python,html,parsing,ip,Python,Html,Parsing,Ip,我已经用谷歌搜索了我的问题,但目前还没有任何解决办法。 我想从这个html内容中获取IP和端口: (我将此内容作为字符串) 我读到了关于靓汤和regexp-我尝试了这两种方法,但都找不到解决方案-而且靓汤非常慢。 感谢我的英语不好 <tr class="proxyListOdd"> <td><a href="http://whois.sc/81.196.122.86" target="_blank">81.196.122.86</a></td

我已经用谷歌搜索了我的问题,但目前还没有任何解决办法。 我想从这个html内容中获取
IP
和端口: (我将此内容作为字符串)

我读到了关于
靓汤
regexp
-我尝试了这两种方法,但都找不到解决方案-而且
靓汤
非常慢。 感谢我的英语不好

<tr class="proxyListOdd">
<td><a href="http://whois.sc/81.196.122.86" target="_blank">81.196.122.86</a></td>
<td>8080</td>
<td>Nein</td>
<td>3</td>
<td class="proxyList_Ping" >0.44 Sek.</td>
<td><img height="24px" width="24px" alt="Rumänien" title="Rumänien" src="http://static2.proxy-listen.de/0_proxy/images/flags/ro.png"></td>
<td class="proxyList_Online arrowUp">97% </td>
<td>22:06</td>
<td><input style="align: center" title="Proxyserver übernehmen" type="image" src="/0_proxy/images/ProxyswitcherButtonOn.png" onclick="de.proxy_listen.setProxy({'U2a66iQA': '70ODEuMTk2LjEyMi44Ng==', 'uhSRlFfS': '96ODA4MA==', 'h0zMxtxH':'21MQ=='}, 'https://addons.mozilla.org/addon/proxy-listen-de_proxyswitcher/');"></td>
<td><a href='proxy:name=Proxy-listen.de&host=81.196.122.86&port=8080&foxyProxyMode=this&confirmation=popup' title="Proxyserver in FoxyProxy übernehmen."><img height="24px" width="22px" alt="FoxyProxy" src="http://static.proxy-listen.de/0_proxy/images/foxyproxy.png"></a></td>
</tr>
<tr class="proxyListEven">
<td><a href="http://whois.sc/94.126.17.68" target="_blank">94.126.17.68</a></td>
<td>3128</td>
<td>Nein</td>
<td>3</td>
<td class="proxyList_Ping" >0.95 Sek.</td>
<td><img height="24px" width="24px" alt="Schweiz" title="Schweiz" src="http://static2.proxy-listen.de/0_proxy/images/flags/ch.png"></td>
<td class="proxyList_Online arrowUp">86% </td>
<td>22:06</td>
<td><input style="align: center" title="Proxyserver übernehmen" type="image" src="/0_proxy/images/ProxyswitcherButtonOn.png" onclick="de.proxy_listen.setProxy({'U2a66iQA': '65OTQuMTI2LjE3LjY4', 'uhSRlFfS': '78MzEyOA==', 'h0zMxtxH':'52MQ=='}, 'https://addons.mozilla.org/addon/proxy-listen-de_proxyswitcher/');"></td>
<td><a href='proxy:name=Proxy-listen.de&host=94.126.17.68&port=3128&foxyProxyMode=this&confirmation=popup' title="Proxyserver in FoxyProxy übernehmen."><img height="24px" width="22px" alt="FoxyProxy" src="http://static.proxy-listen.de/0_proxy/images/foxyproxy.png"></a></td>
</tr>
<tr class="proxyListOdd">
<td><a href="http://whois.sc/89.105.247.13" target="_blank">89.105.247.13</a></td>
<td>3128</td>
<td>Nein</td>

8080
不
3.
0.44瑞典克朗。
97% 
22:06
3128
不
3.
0.95瑞典克朗。
86% 
22:06
3128
不
希望你能帮助我;) 亨利制造商参见

编辑:对于这种特殊情况,您必须执行一些不同的操作,并从这组特定的HTML数据中正则化出数据(因为IP多次出现):

它查找所有
IPv4
地址,若要在其上添加端口,请将其修改为:

re.findall('((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)):([0-9]{1,5})*',data)
应以以下格式查找所有IP和端口:
XXX.XXX.XXX.XXX:YYYYY
(如上所述,它不会检查端口是否有效。

请参阅

编辑:对于这种特殊情况,您必须执行一些不同的操作,并从这组特定的HTML数据中正则化出数据(因为IP多次出现):

它查找所有
IPv4
地址,若要在其上添加端口,请将其修改为:

re.findall('((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)):([0-9]{1,5})*',data)
应以以下格式查找所有IP和端口:
XXX.XXX.XXX.XXX:YYYYY
(如上所述,它不会检查端口是否有效。

使用:

请注意,此正则表达式经过简化,实际上并不捕获所有IP地址(并捕获一些不包含这些地址的值)。如果需要更精确的匹配,请根据和,整个正则表达式如下所示:

# IPv4, common format
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])|
# IPv4, dotted hexadecimal
(?:0x[0-9a-fA-F]{2}\.){3}0x[0-9a-fA-F]{2}|
# IPv4, dotted octal
0[0-7]{3}\.){3}0[0-7]{3}|
# IPv4, one number, hexadecimal
0x[0-9a-fA-F]{1,8})|
# IPv4, one number, octal
0[0-7]{1,11})|
# IPv4, one number, hexadecimal
[1-4][0-9]{9}|0|[1-9][0-9]{0,7}|
# IPv6, preferred form (RFC 4291 2.2.1)
(?:[0-9a-fA-F]{1,4}){7}[0-9a-fA-F]{1,4}|
# IPv6, compressed syntax (RFC 4291 2.2.2)
(?:
  [0-9a-fA-F]{0,4}::(?:[0-9a-fA-F]{1,4}:){,6}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){1}::(?:[0-9a-fA-F]{1,4}:){,4}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){2}::(?:[0-9a-fA-F]{1,4}:){,3}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){3}::(?:[0-9a-fA-F]{1,4}:){,2}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){4}::(?:[0-9a-fA-F]{1,4}:){,1}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){5}::[0-9a-fA-F]{0,4}
)|
# IPv6, alternative form (RFC 4291 2.2.3, uncompressed)
(?:[0-9a-fA-F]{1,4}){6}|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]))|
# IPv6, alternative form (RFC 4291 2.2.3, compressed)
(?:
  [0-9a-fA-F]{0,4}::(?:[0-9a-fA-F]{1,4}:){,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){1}::(?:[0-9a-fA-F]{1,4}:){,3}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){2}::(?:[0-9a-fA-F]{1,4}:){,2}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){3}::(?:[0-9a-fA-F]{1,4}:){,1}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){4}::
)
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]))
如您所见,如果您真的想匹配所有IP地址,您应该搜索近似格式,然后(如有必要)验证地址,例如使用。请注意,上述正则表达式对于您的情况是不完整的,因为它不包括可能的HTML字符编码,例如1的
1;

使用:

请注意,此正则表达式经过简化,实际上并不捕获所有IP地址(并捕获一些不包含这些地址的值)。如果需要更精确的匹配,请根据和,整个正则表达式如下所示:

# IPv4, common format
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])|
# IPv4, dotted hexadecimal
(?:0x[0-9a-fA-F]{2}\.){3}0x[0-9a-fA-F]{2}|
# IPv4, dotted octal
0[0-7]{3}\.){3}0[0-7]{3}|
# IPv4, one number, hexadecimal
0x[0-9a-fA-F]{1,8})|
# IPv4, one number, octal
0[0-7]{1,11})|
# IPv4, one number, hexadecimal
[1-4][0-9]{9}|0|[1-9][0-9]{0,7}|
# IPv6, preferred form (RFC 4291 2.2.1)
(?:[0-9a-fA-F]{1,4}){7}[0-9a-fA-F]{1,4}|
# IPv6, compressed syntax (RFC 4291 2.2.2)
(?:
  [0-9a-fA-F]{0,4}::(?:[0-9a-fA-F]{1,4}:){,6}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){1}::(?:[0-9a-fA-F]{1,4}:){,4}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){2}::(?:[0-9a-fA-F]{1,4}:){,3}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){3}::(?:[0-9a-fA-F]{1,4}:){,2}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){4}::(?:[0-9a-fA-F]{1,4}:){,1}[0-9a-fA-F]{0,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){5}::[0-9a-fA-F]{0,4}
)|
# IPv6, alternative form (RFC 4291 2.2.3, uncompressed)
(?:[0-9a-fA-F]{1,4}){6}|(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]))|
# IPv6, alternative form (RFC 4291 2.2.3, compressed)
(?:
  [0-9a-fA-F]{0,4}::(?:[0-9a-fA-F]{1,4}:){,4}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){1}::(?:[0-9a-fA-F]{1,4}:){,3}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){2}::(?:[0-9a-fA-F]{1,4}:){,2}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){3}::(?:[0-9a-fA-F]{1,4}:){,1}|
  [0-9a-fA-F]{0,4}(?::[0-9a-fA-F]{1,4}){4}::
)
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]))

如您所见,如果您真的想匹配所有IP地址,您应该搜索近似格式,然后(如有必要)验证地址,例如使用。请注意,上述正则表达式对于您的情况是不完整的,因为它不包括可能的HTML字符编码,例如1的
1;

这仅适用于
IPv4

re.findall('(\d+\.\d+\.\d+\.\d+)&port=(\d+)',s)

这仅适用于IPv4:

re.findall('(\d+\.\d+\.\d+\.\d+)&port=(\d+)',s)

您是否考虑过使用文档中的以下内容:


xml.dom.minidom是文档对象模型接口的一个轻量级实现。它比完整的dom更简单,而且要小得多。

您是否考虑过使用文档中类似的东西


xml.dom.minidom是文档对象模型接口的一个轻量级实现。它比完整的dom更简单,而且要小得多。

您有有效的html或xml版本吗?html中的
应该转义到
b.t.w.。您有有效的html或xm版本吗l?html中的
应转义到
&;
b.t.w。