我想用以下页面的class="academicsList"
ul
抓取每个li
的文本:
https://www.eduvision.edu.pk/institutions-detail.php?city=51I&institute=3149_federal-urdu-university-of-arts-science-technology-islamabad
我收到错误:navigateable string has no attribute text
。导致此问题的原因是什么,我该如何解决?
这是我的代码
import requests
from bs4 import BeautifulSoup
from fake_useragent
import UserAgent
ua = UserAgent()
header = {'user-agent':ua.chrome}
response = requests.get('https://www.eduvision.edu.pk/institutions-detail.php?city=51I&institute=3149_federal-urdu-university-of-arts-science-technology-islamabad',headers=header)
soup = BeautifulSoup(response.content, 'html.parser')
disciplines = soup.findAll("ul", {"class": "academicsList"})
for d in disciplines:
for li in d:
print(li.text)
print("...............")
欢迎来到 SO! 您正在尝试迭代找到的 ul 标记。但是你应该迭代 li 标签。为此,您需要在 d 中添加.findAll('li')
。喜欢
for d in disciplines:
for li in d.findAll('li'):
print(li.text)
print("...............")
希望这有帮助!干杯!