英俊聪聪 发表于 2021-2-24 13:56:38

爬虫代理ip

小甲鱼课堂上的代码照抄却老报错???
代码:
import urllib.request
import random

url = 'http://www.whatismyip.com.tw'

iplist = ['171.35.222.70:9999', '101.200.34.46:80', '106.14.39.187:8080']

proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)})

opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36')]

urllib.request.install_opener(opener)

response = urllib.request.urlopen(url)
html = reponse.decode('utf-8')

print(html)

结果:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\urllib\request.py", line 1342, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 1010, in _send_output
    self.send(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 950, in send
    self.connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\http\client.py", line 921, in connect
    self.sock = self._create_connection(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\socket.py", line 843, in create_connection
    raise err
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\lib\socket.py", line 831, in create_connection
    sock.connect(sa)
TimeoutError: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

哪位大佬知道是这么回事???{:5_111:}

昨非 发表于 2021-2-24 14:11:48

免费的代理都是有时效性的
有的已经过期了,这种情况很正常
换个ip试试,看看这个:
https://proxy.mimvp.com/freeopen

南归 发表于 2021-2-24 14:12:42

iplist = ['171.35.222.70:9999', '101.200.34.46:80', '106.14.39.187:8080']
不知道你这个是从哪搞的,
页: [1]
查看完整版本: 爬虫代理ip