问题描述
from bs4 import BeautifulSoup #imports beautifulSoup package
import urllib2
url2 = 'http://www.waldenu.edu/doctoral/phd-in-management/faculty'
page2 = urllib2.urlopen(url2)
soup2 = BeautifulSoup(page2.read(), "lxml")
row2 = soup2.findAll('p')
row2 = row2[18:-4]
names2 = []
arrayNameLength = len(row2)
for x in names2:
current2 = row2[x]
currentString2 = current2.findAll('strong')
if len(currentString2) > 0:
currentString2 = currentString2[0]
names2.append(currentString2.text)
这是我的代码,本质上,我试图从上述站点中刮取教职员工的姓名。
我想我很难从所有名称列表的强标记中获取名称。
1楼
您正在for x in names2:
做for x in names2:
而您的names2
为空白,因此您可能想为for x in row2:
做操作for x in row2:
然后在循环体的后面,您可以将x用作content2,因为x不是索引,它是元素本身
currentString2 = x.findAll('strong')
if len(currentString2) > 0:
currentString2 = currentString2[0]
names2.append(currentString2.text)