gpt4 book ai didi

python - 使用 python 抓取 iFrame

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:20 24 4
gpt4 key购买 nike

通过一些搜索,我发现我想要抓取的内容是在 iframe 内部。这是我总是收到“无”结果的主要原因。我能够开始提取一些数据,例如标题,但是当涉及到表中的数据时,我只能得到第一个结果,即数字 1。这是代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get('http://www.nhl.com/stats/player?aggregate=1&reportType=game&dateFrom=2017-10-20&dateTo=2017-10-31&filter=gamesPlayed,gte,1&sort=shots')
html = driver.page_source
driver.quit()
soup = BeautifulSoup(html,"html.parser")

stat_cat = soup.find('div',attrs={'class':'rt-tr'})
header = stat_cat.text.strip()

stats = soup.find('div',attrs={'class':'rt-td'})
player_stats = stats.text.strip()

print(header,player_stats)

我想要弄清楚的是如何从第二个 soup.find 中获取玩家及其统计数据,但它只返回第一个 rt-td 结果。一旦我拥有了所有数据,我不仅想打印它,还想将其保存到 csv 中。感谢您的浏览!

最佳答案

尝试一下。如果您想从该表中获取所有数据,您可以让它运行脚本。

import csv
import requests

outfile = open("table_data.csv","a",newline='')
writer = csv.writer(outfile)
writer.writerow(["n","m","y","u"])

req = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=true&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22shots%22,%22direction%22:%22DESC%22}]&cayenneExp=gameDate%3E=%222017-10-20%22%20and%20gameDate%3C=%222017-10-31%22%20and%20gameTypeId=2')
data = req.json()['data']
for item in data:
Player = item['playerName']
Pos = item['playerPositionCode']
GP = item['gamesPlayed']
G = item['goals']
A = item['assists']
P = item['points']
Plus_Minus = item['plusMinus']
PIM = item['penaltyMinutes']
PPG = item['ppGoals']
PPP = item['ppPoints']
SHG = item['shGoals']
SHP = item['shPoints']
GWG = item['gameWinningGoals']
OTG = item['otGoals']
S_down = item['shots']
S_per = item['shootingPctg']
TOI = item['timeOnIcePerGame']
Shifts = item['shiftsPerGame']
FOW = item['faceoffWinPctg']
print(Player,Pos,GP,G,A,P,Plus_Minus,PIM,PPG,PPP,SHG,SHP,GWG,OTG,S_down,S_per,TOI,Shifts,FOW)

writer.writerow([Player,Pos,GP,G,A,P,Plus_Minus,PIM,PPG,PPP,SHG,SHP,GWG,OTG,S_down,S_per,TOI,Shifts,FOW])
outfile.close()

部分结果:

Brent Burns D 6 0 5 5 -3 4 0 3 0 0 0 0 31 0.0 1458.8333 29.0 0.0
Max Pacioretty L 5 3 0 3 0 4 0 0 1 1 0 0 29 0.1034 1240.8 26.2 0.0
Phil Kessel R 6 2 4 6 -1 4 0 4 0 0 2 2 27 0.074 1044.3333 21.5 0.3333
Jakub Voracek R 5 2 4 6 2 8 0 0 0 0 0 0 26 0.0769 1191.2 25.4 1.0
John Carlson D 5 0 3 3 -3 2 0 1 0 0 0 0 25 0.0 1686.2 29.4 0.0
Evgeny Kuznetsov C 5 3 1 4 -1 6 0 1 0 0 1 0 24 0.125 1138.4 20.2 0.3703

关于python - 使用 python 抓取 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47068125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com