首页 > 解决方案 > 无法使用 for 循环从网站获取定义列表

问题描述

这里是 python 的新手。我正在尝试获取当天的词汇以及定义和“你知道吗?” 从这里开始https://www.merriam-webster.com/word-of-the-day

知道我错过了什么吗?

到目前为止,我能得到的只有以下内容:

C:\Users\Think\PycharmProjects\wotd\venv\Scripts\python.exe C:/Users/Think/PycharmProjects/wotd/wotd.py
loon
Definition
1 : lout, idler

loon 是单词,后面的行仅是第一个定义。还有3个,但我不能用这种方法得到它们。

感谢您的任何指导

    from bs4 import BeautifulSoup
import requests

sauce = requests.get('https://www.merriam-webster.com/word-of-the-day').text

soup = BeautifulSoup(sauce, 'lxml')

article = soup.find('article')

word = article.find('div', class_='word-and-pronunciation').h1.text
definition = article.find('div', class_='wod-definition-container').h2.text

print(word)
print(definition)

for article in soup.find_all('article'):
    defin = article.find('div', class_='wod-definition-container').p.text
    print(defin)

标签: pythonbeautifulsoup

解决方案


article.find('div', class_='wod-definition-container').p.text只返回拳头

. 像这样试试。

for s in soup.find_all('article')[0].find('div', class_='wod-definition-container').find_all('p'):
    print(s.text)

推荐阅读