ubuntu 解析文件的代理[已关闭]

68bkxrlz  于 2023-01-12  发布在  其他
关注(0)|答案(1)|浏览(75)

此帖子是returned发给超级用户的。当前不接受新答案或交互。Learn more已关闭。此问题需要details or clarity。当前不接受答案。
3天前关闭。
有这个代码

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文件或数据库请帮助我

wgeznvg7

wgeznvg71#

如果我没理解错的话,您希望将所有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

...

相关问题