查找 HTML 页面上的所有 IP

发布于 2024-07-19 14:12:07 字数 257 浏览 2 评论 0原文

我想用 python 获取一个 HTML 页面,然后从中打印出所有 IP。 我将 IP 定义如下:

x.x.x.x:y< /strong>

其中: x = 0 到 256 之间的数字。 y = 一个<<的数字 7 位数字。

谢谢。

I want to get an HTML page with python and then print out all the IPs from it.
I will define an IP as the following:

x.x.x.x:y

Where:
x = a number between 0 and 256.
y = a number with < 7 digits.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独留℉清风醉 2024-07-26 14:12:09

实际操作

\b(?:                # A.B.C in A.B.C.D:port
    (?:
       25[0-5]
    |  2[0-4][0-9]
    |  1[0-9][0-9]
    |  [1-9]?[0-9]
    )\.
  ){3}
  (?:                # D in A.B.C.D:port
    25[0-5]
  | 2[0-4][0-9]
  | 1[0-9][0-9]
  | [1-9]?[0-9]
  )
  :[1-9]\d{0,5}     # port number any number in (0,999999]
\b

In action:

\b(?:                # A.B.C in A.B.C.D:port
    (?:
       25[0-5]
    |  2[0-4][0-9]
    |  1[0-9][0-9]
    |  [1-9]?[0-9]
    )\.
  ){3}
  (?:                # D in A.B.C.D:port
    25[0-5]
  | 2[0-4][0-9]
  | 1[0-9][0-9]
  | [1-9]?[0-9]
  )
  :[1-9]\d{0,5}     # port number any number in (0,999999]
\b
夜吻♂芭芘 2024-07-26 14:12:09

尝试:

re.compile("\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d:\d+").findall(urllib2.urlopen(url).read())

Try:

re.compile("\d?\d?\d.\d?\d?\d.\d?\d?\d.\d?\d?\d:\d+").findall(urllib2.urlopen(url).read())
生生不灭 2024-07-26 14:12:09

并不是要把这变成一场谁是更好的正则表达式作者之战,而是......

(\d{1,3}\.){3}\d{1,3}\:\d{1,6}

Not to turn this into a who's-a-better-regex-author-war but...

(\d{1,3}\.){3}\d{1,3}\:\d{1,6}
酷到爆炸 2024-07-26 14:12:09

基本方法是:

  • 使用 urllib2 下载页面内容
  • 使用 正则表达式 提取类似 IPv4 的地址
  • 根据验证每个匹配项每个八位位组上的数字限制
  • 打印出匹配列表

请提供更清晰的指示,说明您遇到问题的具体部分,并提供证据来表明您到目前为止已经尝试过的内容。

The basic approach would be:

  • Use urllib2 to download the contents of the page
  • Use a regular expression to extract IPv4-like addresses
  • Validate each match according to the numeric constraints on each octet
  • Print out the list of matches

Please provide a clearer indication of what specific part you are having trouble with, along with evidence to show what it is you've tried thus far.

痴梦一场 2024-07-26 14:12:07

对。 我唯一不能做的部分是正则表达式。 – das 9 分钟前 如果有人向我展示这一点,我会没事的。 – das 8 分钟前

import re

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
junk = " 1.1.1.1:123 2.2.2.2:321 312.123.1.12:123 "
print ip.findall(junk)

# outputs ['1.1.1.1:123', '2.2.2.2:321']

这是一个完整的示例:

import re, urllib2

f = urllib2.urlopen("http://www.samair.ru/proxy/ip-address-01.htm")
junk = f.read()

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
print ip.findall(junk)

# ['114.30.47.10:80', '118.228.148.83:80', '119.70.40.101:8080', '12.47.164.114:8888', '121.
# 17.161.114:3128', '122.152.183.103:80', '122.224.171.91:3128', '123.234.32.27:8080', '124.
# 107.85.115:80', '124.247.222.66:6588', '125.76.228.201:808', '128.112.139.75:3128', '128.2
# 08.004.197:3128', '128.233.252.11:3124', '128.233.252.12:3124']

Right. The only part I cant do is the regular expression one. – das 9 mins ago If someone shows me that, I will be fine. – das 8 mins ago

import re

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
junk = " 1.1.1.1:123 2.2.2.2:321 312.123.1.12:123 "
print ip.findall(junk)

# outputs ['1.1.1.1:123', '2.2.2.2:321']

Here is a complete example:

import re, urllib2

f = urllib2.urlopen("http://www.samair.ru/proxy/ip-address-01.htm")
junk = f.read()

ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b")
print ip.findall(junk)

# ['114.30.47.10:80', '118.228.148.83:80', '119.70.40.101:8080', '12.47.164.114:8888', '121.
# 17.161.114:3128', '122.152.183.103:80', '122.224.171.91:3128', '123.234.32.27:8080', '124.
# 107.85.115:80', '124.247.222.66:6588', '125.76.228.201:808', '128.112.139.75:3128', '128.2
# 08.004.197:3128', '128.233.252.11:3124', '128.233.252.12:3124']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文