Python-写txt文件的问题

时间:2019-03-13 01:46:13

标签: python python-3.x python-requests python-textprocessing

我目前正在研究代理刮板,只是出于娱乐目的,但是遇到了问题

我希望将文件输出保存为以下格式的txt文件

185.98.232.22:8080
144.217.161.149:8080
103.106.57.174:8080
49.156.47.61:8080
179.109.144.16:44222

当前,当我尝试写入txt文件时,我仅获得该代理,而没有其他代理,因此我得到了

185.98.232.22:8080

下面是我的代码

from bs4 import BeautifulSoup
import requests

proxyDomain = "https://free-proxy-list.net/"

r = requests.get(proxyDomain)

soup = BeautifulSoup(r.content,"html.parser")

table = soup.find("table",{"id" : "proxylisttable"})

for row in table.find_all("tr"):
    colums = row.find_all("td")
    try:
        print ("%s:%s" % (colums[0].get_text(), colums[1].get_text()))
        Scrapped ="%s:%s" % (colums[0].get_text(), colums[1].get_text())
        file = open("newprox.txt","w")
        file.write(Scrapped)
        file.close()
    except:
        pass

1 个答案:

答案 0 :(得分:0)

更新您的代码以符合以下条件:

    file = open("newprox.txt","a")
    file.write(Scrapped + "\n")