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

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

BeautifulSoup - For循環以錯誤的順序輸出數據

BeautifulSoup - For循環以錯誤的順序輸出數據

手掌心 2023-03-16 15:35:13
我正在嘗試從以下站點抓取匹配數據https://sport-tv-guide.live/live/darts數據正在無誤地抓取,但輸出未按預期顯示。我相信這是因為我錯誤地調用了 for 循環(參見下面的代碼和輸出)import requestsfrom bs4 import BeautifulSoupdef makesoup(url):    cookies = {'mycountries' : '101,28,3,102,42,10,18,4,2'}    r = requests.post(url,  cookies=cookies)    return BeautifulSoup(r.text,"lxml")       def matchscrape(g_data):    for match in g_data:        scheduled = match.findAll('div', class_='main time col-sm-2 hidden-xs')        details = match.findAll('div', class_='col-xs-6 mobile-normal')                for schedule in scheduled:                       print("DateTimes; ", schedule.text.strip())        for detail in details:                print("Details:",  detail.text.strip())                        def matches():    soup=makesoup(url = "https://sport-tv-guide.live/live/darts")    matchscrape(g_data = soup.findAll("div", {"class": "listData"}))上面的代碼提供了以下輸出:然后我嘗試按照下面的代碼更改 for 循環的位置import requestsfrom bs4 import BeautifulSoupdef matchscrape(g_data):    for match in g_data:        scheduled = match.findAll('div', class_='main time col-sm-2 hidden-xs')        details = match.findAll('div', class_='col-xs-6 mobile-normal')                for schedule in scheduled:                       print("DateTimes; ", schedule.text.strip())            for detail in details:                print("Details:",  detail.text.strip())但我收到以下輸出我試圖獲得的輸出是感謝任何可以提供建議或提供解決方案的人。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

使用zip()內置函數將數據“綁定”在一起:


import requests

from bs4 import BeautifulSoup


def makesoup(url):

    cookies = {'mycountries' : '101,28,3,102,42,10,18,4,2'}

    r = requests.post(url,  cookies=cookies)

    return BeautifulSoup(r.text,"lxml")



def matchscrape(g_data):


    for match in g_data:

        scheduled = match.findAll('div', class_='main time col-sm-2 hidden-xs')

        details = match.findAll('div', class_='col-xs-6 mobile-normal')


        for s, d in zip(scheduled, details):  # <-- using zip() here!

            print(s.text.strip())

            print(d.text.strip())



def matches():

    soup=makesoup(url = "https://sport-tv-guide.live/live/darts")

    matchscrape(g_data = soup.findAll("div", {"class": "listData"}))


matches()

印刷:


Darts 17:00

Simon Whitlock vs. Joyce Ryan

World Matchplay

Darts 18:00

Ratajski Krzysztof vs. Wattimena Jermaine

World Matchplay


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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