import requests
import re
import time
import random
sleeptime = random.randint(1,3)
url = "http://api"
headers = {
'Cookie': 'PHPSESSID=m7h0svvpjl29ffspg4g0c8psv1; _ga=GA1.2.221598701.1629504132; _gid=GA1.2.1222632882.1629504132; __gads=ID=ae3c0815acaeadaf-2265598e0dcb0060:T=1629504134:RT=1629504134:S=ALNI_MbG5xPuqFDsLOJ45b1VphPoJJKUiQ; cookieTermsAgreed=1; pws_userid=MjA1NjE%3D; pws_token=ZGQzZmUwYWE4MmE0Y2MzYTIyNzJkN2E5NTYzNjI5ODA%3D; __gpi=00000000-0000-0000-0000-000000000000&d2FsbHBhcGVyZnguY29t&Lw==; __atuvc=4%7C33; __atuvs=6120434cfa7978c8003',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36'
}
response = requests.get(url=url,headers=headers).text
print(response)
ip_adr = response.find(':')-1
port_adr = response.find(':')+1
ip = response[:ip_adr]
port = response[port_adr:]
print(ip + '和' + port)
ip_http = ip + ':' + port
ip_https = "https://" + ip + ':' + port
proxy_dict = {
'http': 'http://' + ip_http,
'https':'https://' + ip_http,
}
try:
response_1 = requests.get(url='https://www.baidu.com/', headers=headers, proxies=proxy_dict, timeout=1)
if response_1.status_code == 200:
print("这个IP代理可以用",proxy_dict)
lis_1.append(proxy_dict)
except:
print(proxy_dict,'IP代理不可用')
返回的是{'http': 'http://13.250.21.2:117150', 'https': 'https://13.250.21.2:117150'} IP代理不可用
以上就是全部代码 (api被我改了)。问一下我这个api地区选的不是美国就是香港,为什么拿到返回的ip地址拿去访问百度都是给我报错,这个是为什么啊,原本是想拿去访问steam市场的,原本以前正常访问直接裸连也是ok的,不知道为什么我试了两个花钱的代理,一个是国内服务器,一个是国外服务器,给我的结果都是报错。想问一下谁遇到过类似的,或者问一下怎么用国外代理来爬取数据不会报错
你这个问题要把try, except 删除看看具体报错原因,怀疑大概率是超时导致的,timeout=1太短了