0

我做了一个如下的解析代码

from bs4 import BeautifulSoup as bs
from pprint import pprint
import requests

html = requests.get('http://a2-m.etoosanswer.co.kr/board/notice/main.do')
soup = bs(html.text, 'html.parser')
data4 = soup.findAll('strong', {'class': 'tit'})

print(data4)

结果是

[<strong class="tit">이투스앤써 [N수생전용] 4월 단과시간표 </strong>, <strong class="tit">[공지] 2021학 
년도 대입일정안내&lt;/strong>, <strong class="tit">이투스앤써 목동점 4월 1주차 주간식단표&lt;/strong>, <strong 
class="tit">[목동앤써] 코로나 19로 인한 '개학연기'에 따른 고3 대응 매뉴얼&lt;/strong>, <strong class="tit"> 
이투스앤써 '실시간 채팅상담' 서비스 오픈!</strong>, <strong class="tit">[이투스앤써] 신종 코로나 바이러스 
감염증 예방을 위한 학원 안전 조치 및 예방 수칙 안내</strong>, <strong class="tit">[이투스앤써] 2021학년도 
6월 평가원 모의고사 취소 안내</strong>, <strong class="tit">재수정규 4월 시작반 설명회 [1부] 영상 
</strong>, <strong class="tit">재수정규 4월 시작반 설명회 [3부] 영상</strong>, <strong class="tit">[입시 
컨설팅] 수시/정시 입시 포트폴리오 수행평가를 위한 자소서 완성</strong>, <strong class="tit">이투스앤써 [N수 
생전용] 4월 단과시간표 </strong>]

像这样..我只想要文本,所以我在 data4 = soup.findAll('strong', {'class': 'tit'}) 旁边添加 .text 命令,我得到了错误.. 我错过了什么吗?

4

1 回答 1

0

返回类型data4是标签列表,而不是您想要的标签 (bs4.element.Tag)。

您需要循环data4以获取所需的文本。

from bs4 import BeautifulSoup as bs
from pprint import pprint
import requests

html = requests.get('http://a2-m.etoosanswer.co.kr/board/notice/main.do')
soup = bs(html.text, 'html.parser')
data4 = soup.findAll('strong', {'class': 'tit'})

for data in data4:
    print(data.text)

输出:

이투스앤써 [N수생전용] 4월 단과시간표 
[공지] 2021학년도 대입일정안내
...
于 2020-04-10T11:04:08.577 回答