selenium设置代理IP爬虫的方法
1:首先生成可以产生新的IP地址的API链接
2:添加IP白名单:
这两步的操作方法详见我的上篇文章:
Request爬虫使用代理的方法
3:使用如下代码即可在使用代理的情况下进行数据的爬取(避免IP被封掉导致爬取不了内容)
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import requests
def get_ip():
url = 'http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=6226c130427f487385ad7b5235bc603c&count=5&expiryDate=0&format=2&newLine=3'
response = requests.get(url)
if response.status_code == 200:
if response.text[0] == '{':
print('获取ip失败')
else:
return [x for x in response.text.split('\n') if x]
else:
print('请求失败')
ips = get_ip()
if ips:
# 添加代理
options = ChromeOptions()
options.add_argument(f'--proxy-server=http://{ips[0]}')
b = webdriver.Chrome(options=options)
b.get('https://cd.fang.anjuke.com/')
print(b.page_source)
else:
print('获取ip失败')