0

我想使用 beautifulSoup从此链接中获取学校名称“Perkins College ...”。

我使用的代码什么也不返回。

school = soup.find('a','profiles-show-school-name-sm-link')
print 'school: ', school
print 'school.text: ', school.text

输出:

school:  <a class="profiles-show-school-name-sm-link" href="/profiles/show/online-degrees/stephen-f-austin-state-university/perkins-college-of-education-undergraduate/395/5401">
<img border="0" src="/images/profiles/243x60/4613/degrees/undergraduate-certificate-in-hospitality-administration.png"/>
</a>
school.text:  

对 BeautifulSoup 实施以提取学校名称(不是 URL)的建议?谢谢!

4

1 回答 1

1
school = soup.find('a','profiles-show-school-name-sm-link')
url = school['href']

假设学校总是在网址中的同一个位置:

for i in range(5):
   url = url[url.find("/")+1:]
schoolname = url[:url.find("/")]
print " ".join(schoolname.split("-")).title()

产量:

Perkins College Of Education Undergraduate

获得大学

for i in range(4):
   url = url[url.find("/")+1:]
university= url[:url.find("/")]
print " ".join(university.split("-")).title()

产量:

Stephen F Austin State University
于 2013-07-31T14:37:43.833 回答