阿匠 发表于 2020-9-26 11:55:05

正则表达式匹配ip地址和端口号

网址是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地址和到端口号,用正则表达式怎么写啊。!!
是不是要用多行匹配和非贪婪匹配啊

suchocolate 发表于 2020-9-27 09:10:26

import re

st1 = '''
<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>
'''

# 1. 分别提取方式
# ip = re.findall(r'"IP">(.*)<', st1)
# port = re.findall(r'PORT">(.*)<', st1)
# print(ip, port)

# 2. 一次提取方式
# result = re.findall(r'"IP">(.*?)<.*?PORT">(.*?)<', st1, re.S)
# print(result)

阿匠 发表于 2020-9-27 12:56:30

本帖最后由 阿匠 于 2020-9-27 12:59 编辑

suchocolate 发表于 2020-9-27 09:10


我想进一步检测某个代理ip能不能用
代码如下
想问状态码正常是不是等于说代理ip可用?
但是为什么用这个ip去打开网页不行
import requests
url='https://www.baidu.com'
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'}

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

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

else:
    print('good')


r=requests.get(url='http://jandan.net/',headers=headers,proxies=proxy)
html=r.text
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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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()
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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))
>>>
================== 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: 由于目标计算机积极拒绝,无法连接。

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: 由于目标计算机积极拒绝,无法连接。

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()
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: 由于目标计算机积极拒绝,无法连接。')))

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: 由于目标计算机积极拒绝,无法连接。')))
>>>
================== 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: 由于目标计算机积极拒绝,无法连接。

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: 由于目标计算机积极拒绝,无法连接。

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()
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: 由于目标计算机积极拒绝,无法连接。')))

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: 由于目标计算机积极拒绝,无法连接。')))
>>>
================== 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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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()
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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))

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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')))















想问状态码正常是不是等于说代理ip可用?
但是为什么用这个ip去打开网页不行

suchocolate 发表于 2020-9-27 13:41:44

状态码判别不出来,你需要找个网站验证,看看你自己的ip是不是,比如:import request
from lxml import etree

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


if __name__ == '__main__':
    main()
页: [1]
查看完整版本: 正则表达式匹配ip地址和端口号