Python Web Scraping:无法从bs4元素中提取文本

时间:2018-05-30 21:07:06

标签: web-scraping beautifulsoup python-requests

我试图从网页中提取姓名和大学座位号码,但我得到以下结果 - [Agnello Fernandes A(1sp15me001)

]
这是一个bs4元素。我需要提取出Agnello Fernandes A'和1sp15me001。

这是我的代码

import requests
from bs4 import BeautifulSoup
r=requests.get("http://cbcs.fastvturesults.com/student/1sp15me001")
c=r.content
soup=BeautifulSoup(c,"html.parser")
all[0].find_all("p",{"class":"card-title page-title mb-0 mt-0"})

如何将其转换为str元素并提取名称?

1 个答案:

答案 0 :(得分:1)

试试这个:

import requests
from bs4 import BeautifulSoup

r = requests.get("http://cbcs.fastvturesults.com/student/1sp15me001")
soup=BeautifulSoup(r.text,"html.parser")
items = soup.find(class_="text-muted")
print("{}\n{}".format(items.previous_sibling,items.text))

输出:

Agnello Fernandes A 
(1sp15me001)