1 回答
TA贡献1802条经验 获得超5个赞
import requests , csv
def SaveAsCsv(list_of_rows,file_name):
try:
print('\nSaving CSV Result')
with open(file_name, 'a', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)
writer.writerow(list_of_rows)
print("rsults saved successully")
except PermissionError:
print(f"Please make sure {file_name} is closed \n")
def fetch_data(url,file_name='test.csv'):
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
if page.status_code == 200 :
soup = BeautifulSoup(page.content, 'lxml')
header = [col_name.text.strip() for col_name in soup.select('table.items thead th')[1:]]
SaveAsCsv(header,file_name)
rows = soup.select('table.items tbody tr')
for row in rows:
name_tag = row.select('img.bilderrahmen-fixed')
if name_tag:
name = name_tag[0].get('title')
tds = row.select('td')[5:]
cols = [ele.text.strip() for ele in tds]
if cols :
cols.insert(0,name)
SaveAsCsv(cols,file_name)
fetch_data('https://www.transfermarkt.com/hapoel-acre/kader/verein/6025/saison_id/2017/plus/1')
添加回答
举报