首页上一页 1 下一页尾页 4 条记录 1/1页
《Python网络爬虫从入门到精通》有学习QQ群吗?现在学习中发现不能正常测试代理IP。
发表在Python图书答疑
2022-03-06
《Python网络爬虫从入门到实践》第5章 请求模块requests 71页-72页
是否精华
是
否
版块置顶:
是
否
《Python网络爬虫从入门到精通》
不能测试代理IP呀

import requests # 导入网络请求模块
import pandas # 导入pandas模块
from lxml import etree # 导入HTML解析模块
ip_table = pandas.read_excel('ip.xlsx') # 读取代理IP文件内容
ip = ip_table['ip'] # 获取代理ip列信息
# 头部信息
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/72.0.3626.121 Safari/537.36'}
# 循环遍历代理IP并通过代理发送网络请求
for i in ip:
proxies = {'http': 'http://{ip}'.format(ip=i),
'https': 'https://{ip}'.format(ip=i)}
try:
# verify=False不验证服务器的SSL证书
response = requests.get('http://2020.ip138.com/',
headers=headers,proxies=proxies,verify=False,timeout=2)
if response.status_code==200: # 判断请求是否成功,请求成功说明代理IP可用
response.encoding='utf-8' # 进行编码
html = etree.HTML(response.text) # 解析HTML
info = html.xpath('/html/body/p[1]/text()')
print(info[0].strip()) # 输出当前ip匿名信息
except Exception as e:
pass
# print('错误异常信息为:',e) # 打印异常信息于2022-03-07 11:12:42编辑

购物车
发表新帖
立即签到












