猿问

BeautifulSoup - For循环以错误的顺序输出数据

我正在尝试从以下站点抓取匹配数据


https://sport-tv-guide.live/live/darts


数据正在无误地抓取,但输出未按预期显示。我相信这是因为我错误地调用了 for 循环(参见下面的代码和输出)


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 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 requests

from bs4 import BeautifulSoup


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

但我收到以下输出

http://img1.mukewang.com/6412c6e30001d9fc04110175.jpg

我试图获得的输出是

http://img2.mukewang.com/6412c6ef0001208004110115.jpg

感谢任何可以提供建议或提供解决方案的人。



手掌心
浏览 71回答 1
1回答

慕运维8079593

使用zip()内置函数将数据“绑定”在一起:import requestsfrom bs4 import BeautifulSoupdef makesoup(url):&nbsp; &nbsp; cookies = {'mycountries' : '101,28,3,102,42,10,18,4,2'}&nbsp; &nbsp; r = requests.post(url,&nbsp; cookies=cookies)&nbsp; &nbsp; return BeautifulSoup(r.text,"lxml")def matchscrape(g_data):&nbsp; &nbsp; for match in g_data:&nbsp; &nbsp; &nbsp; &nbsp; scheduled = match.findAll('div', class_='main time col-sm-2 hidden-xs')&nbsp; &nbsp; &nbsp; &nbsp; details = match.findAll('div', class_='col-xs-6 mobile-normal')&nbsp; &nbsp; &nbsp; &nbsp; for s, d in zip(scheduled, details):&nbsp; # <-- using zip() here!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(s.text.strip())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(d.text.strip())def matches():&nbsp; &nbsp; soup=makesoup(url = "https://sport-tv-guide.live/live/darts")&nbsp; &nbsp; matchscrape(g_data = soup.findAll("div", {"class": "listData"}))matches()印刷:Darts 17:00Simon Whitlock vs. Joyce RyanWorld MatchplayDarts 18:00Ratajski Krzysztof vs. Wattimena JermaineWorld Matchplay
随时随地看视频慕课网APP

相关分类

Python
我要回答