繁体   English   中英

初学者尝试使用 Python 和 BeautifulSoup 刮擦 web

[英]A beginner trying to web scrape using Python and BeautifulSoup

我是所有编码的初学者。

我需要从那个网站上搜集获得全州奖项的高中橄榄球运动员名单。

我深入研究了这个问题,并被引导到 Python 和 Beautiful Soup 到 web 刮擦。

我想出了以下代码,但我很难弄清楚只是获取player information

我得到了一堆标题、链接和添加,但不是我想要的信息。

任何提示将非常感谢。 这是我到目前为止想出的。 善待。

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文件中,我可以将其自动 go 转换为图表格式。 IE( PlayerPositionSchoolHeightWeightYearAward等)

基本上你不需要使用urllib因为 Python 已经有一个很棒的模块requests

如果你想使用print(soup.title.text)那么它会给你页面的title

这是通过class循环遍历特定div的正确方法

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.findAll('div', {"class": "subscriber-only"}):
    print(item.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM