25python成功输出第一个66免费ip代理

# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
# from selenium import webdriver
import subprocess as sp
from lxml import etree
import requests
import random
import re

   #requests的Session可以自动保持cookie,不需要自己维护cookie内容
S = requests.Session()
   #西祠代理高匿IP地址
target_url = 'http://www.66ip.cn/areaindex_15/1.html'
   #完善的headers
target_headers = {'Upgrade-Insecure-Requests':'1',
   'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
   'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
   'Referer':'http://www.66ip.cn/areaindex_15/1.html',
   'Accept-Encoding':'gzip, deflate, sdch',
   'Accept-Language':'zh-CN,zh;q=0.8',
}
   #get请求
target_response = S.get(url = target_url, headers = target_headers)
   #utf-8编码
target_response.encoding = 'utf-8'
   #获取网页信息
target_html = target_response.text
   #获取id为ip_list的table

bf1_ip_list = BeautifulSoup(target_html, 'lxml')




bf2_ip_list = BeautifulSoup(str(bf1_ip_list.find_all(id='footer')), 'lxml')
ip_list_info = bf2_ip_list.table.contents

# 存储代理的列表
proxys_list = []
# 爬取每个代理信息
for index in range(len(ip_list_info)):
    if index % 2 == 1 and index != 1:
        dom = etree.HTML(str(ip_list_info[index]))
        ip = dom.xpath('//td[1]')
        port = dom.xpath('//td[2]')
        protocol = dom.xpath('//td[3]')
        proxys_list.append('http://' + '#' + ip[0].text + '#' + port[0].text)
        print(proxys_list)
# 返回代理列表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值