Some tips you should not have open file in the loop,same for
So a example like this i use with open(close file object automatic).
*https://www.sbostats.com
which is a value that don't change.So a example like this i use with open(close file object automatic).
from bs4 import BeautifulSoup from bs4.dammit import EncodingDetector import requests parser = 'html.parser' # or 'lxml' (preferred) or 'html5lib', if installed resp = requests.get("https://www.sbostats.com/soccer/league/italy/serie-a") http_encoding = resp.encoding if 'charset' in resp.headers.get('content-type', '').lower() else None html_encoding = EncodingDetector.find_declared_encoding(resp.content, is_html=True) encoding = html_encoding or http_encoding soup = BeautifulSoup(resp.content, parser, from_encoding=encoding) table = soup.find_all('table',attrs={'class':'updated_next_results_table'}) table = table[0] tr = table.find_all('tr') base_url = '*https://www.sbostats.com' with open('matches.txt', 'a') as fp: for row in tr: if row.text == None: pass if row.find('a') == None: pass else: #print(' '.join(row.text.replace('STATS', '-').split()[:3])) #print(f"{base_url}{row.find('a')['href']}\n") fp.write(f"{' '.join(row.text.replace('STATS', '-').split()[:3])}\n") fp.write(f"{base_url}{row.find('a')['href']}\n\n")
Output:Verona - Napoli
*https://www.sbostats.com/soccer/stats?country=italy&league=serie-a"e=1.50&direction=away&id=NDAxMTg3OA==
Torino - Inter
*https://www.sbostats.com/soccer/stats?country=italy&league=serie-a"e=1.83&direction=away&id=NDAxMTg3OQ==
Sassuolo - Lazio
*https://www.sbostats.com/soccer/stats?country=italy&league=serie-a"e=2.30&direction=away&id=NDAxMTg4MA==
.....
I like output better like this,but you can just change to have all one line as in your example.Quote:i edited the link for my needs i have only to understand how to remove all numbers of oddsAlso i guess that you have tested all this in the loop,this how i testet only one value(interactive interpret) then added to loop.
>>> tr[2] <tr> <td class="widget-results__team-details ovf updated_m130"> <span class="widget-results__team-name match-name" data-original-title="Verona" data-placement="bottom" data-toggle="tooltip">Verona</span> </td> <td class="widget-results__score text-center limitstats"> <a class="btn btn-primary btn-xs" href='/soccer/stats?country=italy&league=serie-a"e=1.50&direction=away&id=NDAxMTg3OA=='>STATS</a> </td> <td class="widget-results__team-details ovf updated_m130 text-right"> <div class="row"> <div class="col-sm-3"> </div> <div class="col-sm-9"> <span class="widget-results__team-name match-name" data-original-title="Napoli" data-placement="bottom" data-toggle="tooltip"> Napoli </span> </div> </div> </td> <td class="widget-results__quote"> <span class="" style="">6.50</span> </td> <td class="widget-results__quote"> <span class="">4.00</span> </td> <td class="widget-results__quote"> <span class="match_fav" style="">1.50</span> </td> </tr> >>> >>> ' '.join(tr[2].text.replace('STATS', '-').split()) 'Fiorentina - Empoli 1.44 4.33 7.50' >>> # Remove odds >>> ' '.join(tr[2].text.replace('STATS', '-').split()[:3]) 'Verona - Napoli'