鱼C论坛

 找回密码
 立即注册
查看: 1520|回复: 3

[已解决]正则表达式匹配ip地址和端口号

[复制链接]
发表于 2020-9-26 11:55:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
网址是https://www.kuaidaili.com/free/
网页一小段代码如下内容
<tr>
                    <td data-title="IP">125.108.107.95</td>
                    <td data-title="PORT">9000</td>
                    <td data-title="匿名度">高匿名</td>
                    <td data-title="类型">HTTP</td>
                    <td data-title="位置">浙江省温州市  电信</td>
                    <td data-title="响应速度">3秒</td>
                    <td data-title="最后验证时间">2020-09-26 10:31:01</td>
                </tr>
想问 :我要匹配ip地址和到端口号,用正则表达式怎么写啊。!!
是不是要用多行匹配和非贪婪匹配啊
最佳答案
2020-9-27 09:10:26
  1. import re

  2. st1 = '''
  3. <tr>
  4.     <td data-title="IP">125.108.107.95</td>
  5.     <td data-title="PORT">9000</td>
  6.     <td data-title="匿名度">高匿名</td>
  7.     <td data-title="类型">HTTP</td>
  8.     <td data-title="位置">浙江省温州市  电信</td>
  9.     <td data-title="响应速度">3秒</td>
  10.     <td data-title="最后验证时间">2020-09-26 10:31:01</td>
  11. </tr>
  12. '''

  13. # 1. 分别提取方式
  14. # ip = re.findall(r'"IP">(.*)<', st1)[0]
  15. # port = re.findall(r'PORT">(.*)<', st1)[0]
  16. # print(ip, port)

  17. # 2. 一次提取方式
  18. # result = re.findall(r'"IP">(.*?)<.*?PORT">(.*?)<', st1, re.S)[0]
  19. # print(result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-27 09:10:26 | 显示全部楼层    本楼为最佳答案   
  1. import re

  2. st1 = '''
  3. <tr>
  4.     <td data-title="IP">125.108.107.95</td>
  5.     <td data-title="PORT">9000</td>
  6.     <td data-title="匿名度">高匿名</td>
  7.     <td data-title="类型">HTTP</td>
  8.     <td data-title="位置">浙江省温州市  电信</td>
  9.     <td data-title="响应速度">3秒</td>
  10.     <td data-title="最后验证时间">2020-09-26 10:31:01</td>
  11. </tr>
  12. '''

  13. # 1. 分别提取方式
  14. # ip = re.findall(r'"IP">(.*)<', st1)[0]
  15. # port = re.findall(r'PORT">(.*)<', st1)[0]
  16. # print(ip, port)

  17. # 2. 一次提取方式
  18. # result = re.findall(r'"IP">(.*?)<.*?PORT">(.*?)<', st1, re.S)[0]
  19. # print(result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-27 12:56:30 | 显示全部楼层
本帖最后由 阿匠 于 2020-9-27 12:59 编辑


我想进一步检测某个代理ip能不能用
代码如下
想问状态码正常是不是等于说代理ip可用?
但是为什么用这个ip去打开网页不行
  1. import requests
  2. url='https://www.baidu.com'
  3. headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51'}

  4. proxy={'http':'123.55.101.73:9999'}

  5. response=requests.get(url=url,headers=headers,proxies=proxy)
  6. if response.status_code!=200:
  7.     print('bad')

  8. else:
  9.     print('good')


  10. r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
  11. html=r.text
  12. print(html)
复制代码

然后会打印good 然后报错
Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\python\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\python\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\python\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000000003B016D8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\python\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='125.110.69.22', port=9000): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003B016D8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wmx\Desktop\testip.py", line 16, in <module>
    r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
  File "D:\python\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='125.110.69.22', port=9000): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003B016D8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
>>>
================== RESTART: C:\Users\wmx\Desktop\testip.py ==================
good
Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\python\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\python\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\python\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x00000000039A1630>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\python\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='112.111.217.165', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000000039A1630>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wmx\Desktop\testip.py", line 16, in <module>
    r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
  File "D:\python\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='112.111.217.165', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000000039A1630>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
>>>
================== RESTART: C:\Users\wmx\Desktop\testip.py ==================
good
Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\python\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\python\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\python\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000000003B516D8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\python\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='112.111.217.165', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003B516D8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wmx\Desktop\testip.py", line 16, in <module>
    r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
  File "D:\python\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='112.111.217.165', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003B516D8>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
>>>
================== RESTART: C:\Users\wmx\Desktop\testip.py ==================
good
Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection
    raise err
  File "D:\python\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\python\lib\http\client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "D:\python\lib\http\client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "D:\python\lib\http\client.py", line 1016, in _send_output
    self.send(msg)
  File "D:\python\lib\http\client.py", line 956, in send
    self.connect()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 187, in connect
    conn = self._new_conn()
  File "D:\python\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000000003A414A8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\python\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "D:\python\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='123.55.101.73', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003A414A8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wmx\Desktop\testip.py", line 16, in <module>
    r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
  File "D:\python\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "D:\python\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\python\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "D:\python\lib\site-packages\requests\adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='123.55.101.73', port=9999): Max retries exceeded with url: http://jandan.net/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003A414A8>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))















想问状态码正常是不是等于说代理ip可用?
但是为什么用这个ip去打开网页不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-27 13:41:44 | 显示全部楼层
状态码判别不出来,你需要找个网站验证,看看你自己的ip是不是,比如:
  1. import request
  2. from lxml import etree

  3. def main():
  4.     proxy = {'http': 'http://1.1.1.1:8080'}
  5.     url = 'https://my.ip.cn/api/index?ip=&type=0'
  6.     headers = {'user-agent': 'firefox', 'Referer': 'https://my.ip.cn/', 'X-Requested-With': 'XMLHttpRequest'}
  7.     r = requests.get(url, headers=headers, proxies=proxy)
  8.     html = etree.HTML(r.text)
  9.     result = html.xpath('//p//text()')
  10.     print(result)


  11. if __name__ == '__main__':
  12.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-3 04:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表