python - 使用 Python 抓取网页时从链接中提取 href
问题描述
我正在从这个页面抓取: https ://www.pro-football-reference.com/years/2018/week_1.htm
这是美式足球比赛得分的列表。我想打开第一场比赛的数据链接。显示的文字为“最终”。到目前为止我的代码...
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
#assigning url
my_url = "https://www.pro-football-reference.com/years/2018/week_1.htm"
# opening up connection, grabbing the page
raw_page = uReq(my_url)
page_html = raw_page.read()
raw_page.close()
# html parsing
page_soup = soup(page_html,"html.parser")
#find all games on page
games = page_soup.findAll("div",{"class":"game_summary expanded nohover"})
link = games[0].find("td",{"class":"right gamelink"})
print(link)
当我运行它时,我收到以下输出...
<a href="/boxscores/201809060phi.htm">Final</a>
如何仅将链接文本(即“/boxscores/201809060phi.htm”)分配给变量?
解决方案
link = games[0].find("td",{"class":"right gamelink"}).find('a')
print(link['href'])
推荐阅读
- angular - 无法读取 AdminProductsComponent.onclickfetch() 处未定义的属性“订阅”
- fiware - IoTAgent 无法向 Orion CB 发送通知
- arrays - Laravel 使用点符号合并两个数组
- javascript - ng-repeat 中的 ng-options 删除已选择的选项值
- symfony - 奏鸣曲安装错误:“不存在的参数”
- haskell - haskell 中的 SomeException 数据类型
- docker - 在 docker-compose.yml 中使用 Linux 容器在 Windows 主机上挂载 Docker 卷
- css - 如何将图像与文本保持在同一行?
- java - Tomcat中的自定义用户库文件夹
- java - Google App Engine 的 Vaadin 应用程序问题