到目前为止,我的代码是:
year = range(1958,2013)
randomYear = random.choice(year)
randomYear = str(randomYear)
page = range(1,5)
randomPage = random.choice(page)
randomPage = str(randomPage)
print(randomPage, randomYear)
url = 'http://www.billboard.com/artists/top-100/'+randomYear+'?page='+randomPage
url1 = urlopen(url)
htmlSource = url1.read()
url1.close()
soup = BeautifulSoup(htmlSource)
listm = soup.findAll('article', {'class': 'masonry-brick','style' : 'position; absolute; top; 0px; left: 0px;'})
for listm in soup.findAll('div',{'class': 'thumbnail'}):
for listm in soup.find('img alt')(''):
print(listm)
我要做的是获取img alt=''文本。我想我是对的,但它什么也没显示。
我想你的意思是:
这将找到一个
img
标记,其属性alt
的值为''
(无)要获取具有
alt
属性的<img>
元素,可以使用soup('img', alt=True)
:不要为不同的目的使用相同的名称,这会损害代码的可读性:
注:
articles
未使用。可以使用
.find()
方法,为每个<div>
获取一个<img>
元素:相关问题 更多 >
编程相关推荐