vendredi 18 août 2017

Replace text tags inside txt file python3

Im trying to make a proxy scrapper, this is my code:

import bs4
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as soup
import lxml
from contextlib import redirect_stdout

meh=[]

pathf = '/home/user/tests.txt'

url = Request('http://ift.tt/2uXDYCx', headers={'User-Agent': 'Mozilla/5.0'})

page_html = urlopen(url).read()

page_soup = soup(page_html, features="xml")

final = page_soup.tbody

meh.append(final)

with open(pathf, 'w') as f:
    with redirect_stdout(f):
        print(meh[0].text.strip())

Now i want the text to show in a more readable way, because its like this:

12.183.20.3615893USUnited StatesSocks5AnonymousYes11 seconds ago220.133.97.7445657TWTaiwanSocks5AnonymousYes11 seconds ago

How can i turn this text into a more readable file? something like:

12.183.20.36 15893 US United States Socks5 Anonymous Yes 11 seconds ago (new line) ...




Aucun commentaire:

Enregistrer un commentaire