【问题标题】:Parse proxy to file解析代理到文件
【发布时间】:2023-01-09 01:38:14
【问题描述】:

有这段代码

import requests
from bs4 import BeautifulSoup


ports_url = 'http://spys.one/proxy-port/'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}

soup = BeautifulSoup(requests.post(ports_url, headers=headers, data={'xpp': 5}).content, 'html.parser')
for f in soup.select('td[colspan="2"] > a > font.spy6'):
    u = 'http://spys.one/proxy-port/' + f.text + '/'
    s = BeautifulSoup(requests.post(u, headers=headers, data={'xpp': 5}).content, 'html.parser')
    for ff in s.select('tr > td:nth-child(1) > font.spy14'):
        print(ff.text)

有必要将 ip=ip : port=port 保存到 txt 文件或 db 请帮帮我

【问题讨论】:

    标签: linux ubuntu python parsing


    【解决方案1】:

    如果我理解正确的话,您想将所有 IP 和端口保存到一个文件中(例如 CSV):

    import csv
    
    import requests
    from bs4 import BeautifulSoup
    
    ports_url = "http://spys.one/proxy-port/"
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"
    }
    
    all_data = []
    soup = BeautifulSoup(
        requests.post(ports_url, headers=headers, data={"xpp": 5}).content, "html.parser"
    )
    for f in soup.select('td[colspan="2"] > a > font.spy6'):
        u = "http://spys.one/proxy-port/" + f.text + "/"
        s = BeautifulSoup(
            requests.post(u, headers=headers, data={"xpp": 5}).content, "html.parser"
        )
        for ff in s.select("tr > td:nth-child(1) > font.spy14"):
            ip, port = ff.text.split(":")
            all_data.append((ip, port))
    
    with open("data.csv", "w") as f_out:
        writer = csv.writer(f_out)
        writer.writerow(["IP", "PORT"])
        writer.writerows(all_data)
    

    这将创建data.csv

    IP,PORT
    190.61.88.147,8080
    14.207.202.42,8080
    160.119.148.19,8080
    178.212.54.137,8080
    176.88.55.195,8080
    86.109.33.38,8080
    2.179.167.22,8080
    97.107.142.202,8080
    8.210.52.87,8080
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 2014-01-20
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多