【发布时间】:2020-06-24 01:24:30
【问题描述】:
我正在尝试从出现在标签内(和之后)的类中提取文本,如下所示:
from bs4 import BeautifulSoup
html = """<div class="wisbb_teamA">
<a href="http://www.example.com/eg1" class="wisbb_name">Phillies</a>
</div>"""
soup = BeautifulSoup(html,"lxml")
for div in soup.findAll('div', attrs={'class':'wisbb_teamA'}):
print(div.find('a').contents[0])
这将返回以下内容:
Phillies
这是正确的,但是当我尝试从实际页面中提取时,我得到以下信息:
TypeError: object of type 'Response' has no len()
页面在
https://www.foxsports.com/mlb/scores?season=2019&date=2019-09-23
我使用了以下内容:
import requests
from bs4 import BeautifulSoup
url = requests.get("https://www.foxsports.com/mlb/scores?season=2019&date=2019-09-23")
soup = BeautifulSoup(url,'lxml')
for div in soup.findAll('div', attrs={'class':'wisbb_teamA'}):
print(div.find('a').contents[0])
谢谢。
【问题讨论】:
-
试试这个:soup = BeautifulSoup(url.text,'lxml')
-
@Matt,你到底在追求什么?可能有一种更简单的方法可以通过 api 获取它。
-
@chitown88 - 正在寻找打印团队名称。我也会试试你的解决方案。
标签: html python-3.x beautifulsoup