1 回答

TA貢獻1824條經驗 獲得超6個贊
試試這個:
from bs4 import BeautifulSoup as bs
html = '<div class="row"><div class="col-md-12"><div class="panel panel-default"><div class="panel-body "><div class="row"><div id="ContentPlaceHolder1_pnldefault"><table id="ContentPlaceHolder1_dlstCollege" class="table table-bordered table responsive" cellspacing="0" style="border-collapse:collapse;"><tr><td><input type="hidden" name="ctl00$ContentPlaceHolder1$dlstCollege$ctl00$hdnInstituteId" id="ContentPlaceHolder1_dlstCollege_hdnInstituteId_0" value="968 " /><a id="ContentPlaceHolder1_dlstCollege_hlpkInstituteName_0" href="CollegeDetailedInformation.aspx?Inst=968 ">**A R INSTITUTE OF PHARMACY , BIJNOR (968)**</a><br /><b>Location:</b><span id="ContentPlaceHolder1_dlstCollege_lblAddress_0">**TAJPUR** </span><br /><b>Course:</b><span id="ContentPlaceHolder1_dlstCollege_lblCourse_0">**B.Pharm**,</span><br /><b>Category:</b><span id="ContentPlaceHolder1_dlstCollege_lblInstituteType_0">**Private**</span><br /><b>Web Address:</b><a id="lnkBtnWebURL" href='' target="_blank"></a><br /></td></tr>'
soup = bs(html , 'lxml')
name = soup.find('a', id='ContentPlaceHolder1_dlstCollege_hlpkInstituteName_0').text.strip()
address = soup.find('span', id= 'ContentPlaceHolder1_dlstCollege_lblAddress_0').text.strip()
course = soup.find('span', id = 'ContentPlaceHolder1_dlstCollege_lblCourse_0').text.strip()
institute_type = soup.find('span', id = 'ContentPlaceHolder1_dlstCollege_lblInstituteType_0').text.strip()
print(name)
print(address)
print(course)
print(institute_type)
輸出:
**A R INSTITUTE OF PHARMACY , BIJNOR (968)**
**TAJPUR**
**B.Pharm**,
**Private**
添加回答
舉報