【发布时间】:2021-12-09 09:03:29
【问题描述】:
我的项目涉及我从https://www.randomlists.com/random-vocabulary-words 中抓取随机单词并使用从网站上抓取的单词创建一个多项选择题。
我需要抓取的数据位于 <div> 中,其中 class="Rand-stage",特别是它下面的 <ol> 标签。完成此操作后,我需要获取<ol> 下的<li> 标签中的单词。我附上了这张图片。 Image
目前,我的代码如下:
url = https://www.randomlists.com/random-vocabulary-words
r = requests.get(url)
html_content = r.content
soup = bs4.BeautifulSoup(html_content, 'html.parser')
result = []
for li in soup.find('ol', class_='rand_large').find_all('li'):
result.append(list(li.stripped_strings))
print(result)
现在,我不知道如何抓取 <ol> 标签中的内容,或者如果这是我首先需要抓取的内容,以获得随机单词(及其含义,也在<li>标签中)。
实际上,当代码运行时,它并没有显示任何输出。相反,它抛出了一个
错误(AttributeError: 'NoneType' 对象没有属性 'find_all')
【问题讨论】:
-
看起来
rand_large是- 标签中的一个类,而不是
- 标签,所以第一个
soup.find应该找不到任何东西。只是在做soup.find_all('li')时,你是不是捡了太多垃圾? - 标签中的一个类,而不是
-
感谢您的回复,实际上,当代码运行时,它并没有显示任何输出。相反,它抛出了一个错误(AttributeError: 'NoneType' object has no attribute 'find_all')。
标签: python web-scraping beautifulsoup