我花了一整天的时间在互联网上调查我的问题,但我找不到任何解决方案,所以,因此,我向你提出我的问题:
我成功地编写了一个代码来抓取特定信息的特定URL。现在,我想增强代码,以抓取相同信息的多个URL。
- 网站URL在文本结尾处相差一个单词。我写了一个代码来建立一个URL列表。对于这个例子,我将其缩减为两个网站(changingignwordinurl)。
- 我想把这两个网站的“名字”、“摸球”和“黄牌”的数据刮出来
- the name can be found on both websites
- the amount of balls touched can be found on (changingignwordinurl = ballkontakte) and
- the amount of yellow cards can be found on the second website (changingignwordinurl = fairplay)
- 我的目标是列出所有球员的名字和他们触球的次数,下面是所有吃到黄牌的球员和黄牌的数量
你们专业人士中有谁能指出我的错误吗?非常感谢您的帮助:)。
from bs4 import BeautifulSoup
import requests
import pandas as pd
urllist = []
changingignwordinurl = ["-ballkontakte/","-fairplay/",]
rooturl = "https://sportdaten.spiegel.de/fussball/bundesliga/ma9417803/fc-augsburg_eintracht-frankfurt/spielstatistik"
for word in changingignwordinurl:
urllist.append(rooturl+word)
playerdata = []
for url in urllist:
def get_data(url):
response = requests.get(urllist)
soup = BeautifulSoup(response.content,"lxml")
players = soup.find("table",
class_="module-statistics statistics")
for player in players:
item={}
name = player.find("td", class_="person-name")
ballstouchedtotal = player.find("td", class_="person_stats-balls_touched person_stats-balls_touched-list")
yellowcards = player.find("td", class_= "person_stats-card_yellow person_stats-card_yellow-list")
item["Name"] = name.text.strip() if name else ""
item["Balls touched"] = ballstouchedtotal.text.strip() if ballstouchedtotal else ""
item["Yellow Card"] = yellowcards.text.strip() if yellowcards else ""
data.append(item)
return playerdata
print(playerdata)
2条答案
按热度按时间tyky79it1#
您已经定义了函数
get_data()
,但它没有被调用。考虑将函数定义移出循环,并将其替换为函数调用get_data(url)
。试试类似的东西:
rseugnpd2#
这是我的解决方案。