亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用美麗湯從網站獲取特定的子類?

如何使用美麗湯從網站獲取特定的子類?

開心每一天1111 2022-09-20 15:41:10
我想從這個鏈接中獲取表格: 足球運動員市場價值 我設法這樣做了,用這個代碼:def few(urls, file):f = open(file, 'a', newline='', encoding="utf-8")writer = csv.writer(f)url = urlspage = 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”),而不是它包含的所有數據。你能幫忙嗎?謝謝!
查看完整描述

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')

http://img1.sycdn.imooc.com//63296ebc0001c14915110881.jpg

查看完整回答
反對 回復 2022-09-20
  • 1 回答
  • 0 關注
  • 78 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號