初学者尝试使用Python和BeautifulSoup进行网页抓取

问题描述 投票:-1回答:1

我是所有编码的完全入门。我需要从https://cumberlink.com/sports/high-school/football/pa-football-writers-all-state-team-class-a-a-and/article_4d286757-a501-5b5b-b3be-cfebc06ef455.html中获得获得全州奖项的高中足球运动员名单。我深入研究这个问题,并被引向Python和Beautiful Soup网站抓取。我想出了下面的代码,但是我很难弄清楚只获取玩家信息。我得到了大量的标题,链接和添加信息,但没有我想要的信息。任何提示将非常感谢。到目前为止,这是我想出的。善待...

import urllib
import urllib.request
from bs4 import BeautifulSoup

theurl = "https://cumberlink.com/sports/high-school/football/pa-football-writers-all-state-team- 
class-a-a-and/article_4d286757-a501-5b5b-b3be-cfebc06ef455.html"
thepage = urllib.request.urlopen (theurl)
soup = BeautifulSoup (thepage, "html.parser")

print (soup.title.text)

""""""
for link in soup.findAll('p'):
   print (link.get('href'))
   print (link.text)

""""""
print (soup.find('div', {"class":"subscriber-only"}))

[此外,如果有人可以帮助我了解如何将其导入到excel文件中,我可以自动将其转换为图表格式。即(玩家,位置,学校,身高,体重,年份,奖项等)

python python-3.x beautifulsoup
1个回答
0
投票
import requests
from bs4 import BeautifulSoup

r = requests.get('https://cumberlink.com/sports/high-school/football/pa-football-writers-all-state-team-class-a-a-and/article_4d286757-a501-5b5b-b3be-cfebc06ef455.html').text
soup = BeautifulSoup(r, 'html.parser')

for item in soup.find_all('div', {"class": "subscriber-only"}):
    print(item.text)
© www.soinside.com 2019 - 2024. All rights reserved.