Python I';我正在尝试从网页上抓取一个网页,并将数据存储在CSV文件中。但我可以';我的代码似乎无法正常工作

Python I';我正在尝试从网页上抓取一个网页,并将数据存储在CSV文件中。但我可以';我的代码似乎无法正常工作,python,web-scraping,Python,Web Scraping,我正试图从这个链接中获取网页内容, 而我似乎并不成功 下面是我试过的代码 from urllib.request import urlopen from bs4 import BeautifulSoup import requests import csv` url = "https://www.premierleague.com/stats/top/players/goals?se=-1" html = urlopen(url) bs = BeautifulSoup(ht

我正试图从这个链接中获取网页内容, 而我似乎并不成功

下面是我试过的代码

from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
import csv`

url = "https://www.premierleague.com/stats/top/players/goals?se=-1"
html = urlopen(url)
bs = BeautifulSoup(html, 'html.parser')

#print(bs)

listings = []

for rows in bs.find_all("tr"):
 if("oddrow" in rows["class"]) or ("evenrow" in rows["class"]):
    name = rows.find("div", class_="playerName").a.get_text()
    country = rows.find_all("td")[1].get_text()
    goals = rows.find_all("td")[4].get_text()
    listings.append([name, country, goals])

with open("EPL_TEST.csv", 'a', encoding = 'utf-8') as toWrite:
    writer = csv.writer(toWrite)
    writer.writerows(listings)

print("Data Fetched")
这就是我遇到的错误:
C:\Users\Siddhardh\Desktop\Python\Projects\FinalProject\venv\Scripts\Python.exe C:/Users/Siddhardh/Desktop/Python/Projects/FinalProject/Scraping.py
Traceback(最后一次调用):文件“C:/Users/Siddhardh/Desktop/Python/Projects/FinalProject/Scraping.py”,第16行,在
if(“oddrow”在第[“class”]行中)或(“evenrow”在第[“class”]行中):文件“C:\Users\Siddhardh\Desktop\Python\Projects\FinalProject\venv\lib\site packages\bs4\element.py”,第1016行,在uu getitem\uuuuuuuuuu返回self.attrs[key]keyror:“class”

过程结束,退出代码为1

我需要将所有球员的姓名、国家和目标输入CSV文件


请原谅我的编辑技巧。这是我在这里的第一篇文章。我会学习的。

看起来您必须将代码的中间部分更改为:

listings = []

names = bs.find_all("td",scopr="row")
countries = bs.find_all("span",  {"class": "playerCountry"})
goals = bs.find_all("td",class_="mainStat")
for name, country, goal in zip(names,countries,goals):    
    listings.append([name.text.strip(), country.text.strip(), goal.text.strip()])
打印出
列表
会产生以下输出:

['Alan Shearer','England','260'] ['Wayne Rooney','England','208'] ['Andrew Cole','England','187']


等等。

请尝试下面的脚本,以获取跨越多个页面的所有名称以及数据丰富的csv文件。您可以使用chrome开发工具获取我在脚本中使用的链接。使用该链接,您将获得json响应。修改以获取所有其他字段

import csv
import requests
from bs4 import BeautifulSoup

url = "https://footballapi.pulselive.com/football/stats/ranked/players/goals?page={}&pageSize=20&comps=1&compCodeForActivePlayer=EN_PR&altIds=true"

headers = {
    'Origin': 'https://www.premierleague.com',
}

def get_items(link,page):
    while True:
        res = requests.get(link.format(page),headers=headers)
        soup = BeautifulSoup(res.text,"lxml")
        if not len(res.json()['stats']['content']):break
        for item in res.json()['stats']['content']:
            player_name = item['owner']['name']['display']
            yield player_name

        page+=1

if __name__ == '__main__':
    page = 112
    with open("player_info.csv","w", newline="") as outfile:
        writer = csv.writer(outfile)
        writer.writerow(['page','player'])
        for name in get_items(url,page):
            writer.writerow([name])

我使用
page=112
获取从该页开始的所有名称。请随意设置
0
,以从头到尾获取姓名。

您是否查看了Siddarth Krishna S的任一答案?人们把时间花在解决问题上,所以不要无动于衷。谢谢,对不起。是的,人们一直在帮助我们。我刚出城,没带笔记本电脑。刚回来看看这些评论。是的。这就是我需要它和你的代码的工作方式。但是我的CSV只包含一个页面。我需要在多个页面上做,我尝试了循环的疤痕。不起作用。所以我修改了国家和目标,然后添加到CSV中,就像你在原始答复中所做的那样,对吗?它似乎不起作用。你能帮我一下吗?不清楚你所说的
似乎不起作用,因为我发现@Siddarth Krishna s.它的工作方式是正确的。你是否得到了超过第1页的所有玩家的名字?