fg2312

python获取代理IP

利用requests库获取代理,用Beautiful库解析网页筛选ip

# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from threading import Thread

headers = {\'user-agent\': \'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0\'}


#定义获取IP函数
def get_ip():
   #写入txt
    write_ip = open(\'get_ip.txt\', \'w\')
    for page in range(1, 10):
        url = \'http://www.xicidaili.com/nn/%s\' % page
        r = requests.get(url, headers=headers,timeout=5)

        # 用beautifulsoup库解析网页
        soup = BeautifulSoup(r.content, \'lxml\')
        trs = soup.find(\'table\', id=\'ip_list\').find_all(\'tr\')

        for tr in trs[1:]:
            tds = tr.find_all(\'td\')
            ip = tds[1].text.strip()
            port = tds[2].text.strip()
write_ip.write(\'%s\n\'%(ip+\':\'+port)) write_ip.close()
print(\'done\')

get_ip()

 

分类:

技术点:

相关文章:

  • 2021-11-12
  • 2021-11-02
  • 2021-11-02
  • 2021-11-02
  • 2021-12-06
  • 2021-11-02
  • 2021-11-02
猜你喜欢
  • 2021-11-06
  • 2021-11-02
  • 2022-01-02
  • 2021-07-22
  • 2021-10-28
  • 2021-10-11
  • 2021-08-05
相关资源
相似解决方案