如何使用美丽汤从网站获取特定的子类?

我想从这个链接中获取表格: 足球运动员市场价值 我设法这样做了,用这个代码:


def few(urls, file):

f = open(file, 'a', newline='', encoding="utf-8")

writer = csv.writer(f)

url = urls

page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})

soup = BeautifulSoup(page.content, 'lxml')

tbody = soup('table', {"class": "items"})[0].find_all('tr')

for row in tbody:

    cols = row.findChildren(recursive=False)[1:]

    exclude = [0, 3, 4, 6, 7, 8]

    cols = [ele.text.strip() for ele in cols[:] if ele not in exclude]

    writer.writerow(cols)

我的问题是,从第一列开始,我只想得到名字(例如:“Ram Strauss”),而不是它包含的所有数据。


你能帮忙吗?谢谢!


开心每一天1111
浏览 64回答 1
1回答

慕后森

import requests , csvdef 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')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python