我對 python 比較陌生,只做過資料幀分析的專案。我正在嘗試學習網路抓取以完成個人專案。
我正在練習這些基礎知識,這是我當前的代碼:
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import requests
html_text = requests.get('https://www.pff.com/news/nfl-quarterback-rankings-all-32-starters-ahead-of-the-2021-nfl-season').text
soup = BeautifulSoup(html_text,'lxml')
players = soup.find('div',class_ = 'm-longform-copy')
for player in players:
name = players.h3.a.text
print(name)
當我運行它時,它只是重復列印“Patrick Mahomes”而不是進入下一個條目。我在此處查找了其他一些類似的問題,但不太了解語法以將其應用于我的問題。任何幫助都會很棒!
uj5u.com熱心網友回復:
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import requests
html_text = requests.get('https://www.pff.com/news/nfl-quarterback-rankings-all-32-starters-ahead-of-the-2021-nfl-season').text
soup = BeautifulSoup(html_text,'lxml')
players = soup.select('h3 ')
for player in players:
name = player.text
print(name)
輸出:
1. Patrick Mahomes, Kansas City Chiefs
2. Tom Brady, Tampa Bay Buccaneers
3. Aaron Rodgers, Green Bay Packers
4. Russell Wilson, Seattle Seahawks
5. Deshaun Watson, Houston Texans
6. Josh Allen, Buffalo Bills
7. Dak Prescott, Dallas Cowboys
8. Lamar Jackson, Baltimore Ravens
9. Matt Ryan, Atlanta Falcons
10. Baker Mayfield, Cleveland Browns
11. Matthew Stafford, Los Angeles Rams
12. Ryan Tannehill, Tennessee Titans
13. Derek Carr, Las Vegas Raiders
14. Kirk Cousins, Minnesota Vikings
15. Justin Herbert, Los Angeles Chargers
16. Ben Roethlisberger, Pittsburgh Steelers
17. Kyler Murray, Arizona Cardinals
18. Joe Burrow, Cincinnati Bengals
19. Ryan Fitzpatrick, Washington Football Team
20. Daniel Jones, New York Giants
21. Trevor Lawrence, Jacksonville Jaguars
22. Jimmy Garoppolo, San Francisco 49ers
23. Carson Wentz, Indianapolis Colts
24. Jameis Winston/Taysom Hill, New Orleans Saints
25. Justin Fields, Chicago Bears
26. Jared Goff, Detroit Lions
27. Cam Newton, New England Patriots
28. Sam Darnold, Carolina Panthers
29. Tua Tagovailoa, Miami Dolphins
30. Zach Wilson, New York Jets
31. Jalen Hurts, Philadelphia Eagles
32. Drew Lock, Denver Broncos
NFL Featured Tools
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346796.html
