【问题标题】:creating a dictionary from two loop variables从两个循环变量创建字典
【发布时间】:2021-08-18 23:23:30
【问题描述】:

我想在 python 中创建一个字典应用程序,代码如下:

from bs4 import BeautifulSoup
import requests
URL="https://www.vocabulary.com/lists/176046"
page=requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
corrupted=soup.find_all(attrs={'class':'word dynamictext'})
corrupted_2=soup.find_all(attrs={'class':'definition'})  
for i in corrupted:
    words=i.text
for x in corrupted_2:
    definitions=x.text

my_dict={words,definitions}
print(my_dict)
    

我的问题是,当我打印 my_dict 时,我只能从变量 words 中得到最后一个单词 有人可以告诉我如何解决这个问题吗?感谢您的帮助

【问题讨论】:

  • 你只要继续覆盖变量,只需追加到列表中,你的问题就会得到解决!

标签: python loops beautifulsoup python-requests


【解决方案1】:

可以使用zip()内置函数+dict()构造字典:

import requests
from bs4 import BeautifulSoup

URL = "https://www.vocabulary.com/lists/176046"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")

corrupted = [t.text for t in soup.find_all(attrs={"class": "word dynamictext"})]
corrupted_2 = [t.text for t in soup.find_all(attrs={"class": "definition"})]

my_dict = dict(zip(corrupted, corrupted_2))
print(my_dict)

打印:

{
    "aberration": "a state or condition markedly different from the norm",
    "abhor": "find repugnant",
    "acquiesce": "agree or express agreement",
    "alacrity": "liveliness and eagerness",
    "amiable": "diffusing warmth and friendliness",
    "appease": "make peace with",
    "arcane": "requiring secret or mysterious knowledge",
    "avarice": "reprehensible acquisitiveness; insatiable desire for wealth",
    "brazen": "unrestrained by convention or propriety",
    "brusque": "rudely abrupt or blunt in speech or manner",
    "cajole": "influence or urge by gentle urging, caressing, or flattering",
    "callous": "emotionally hardened",
    "candor": "the quality of being honest and straightforward",
    "chide": "scold or reprimand severely or angrily",
    "circumspect": "careful to consider potential consequences and avoid risk",
    "coerce": "cause to do through pressure or necessity",
    "coherent": "marked by an orderly and consistent relation of parts",
    "complacency": "the feeling you have when you are satisfied with yourself",
    "confidant": "someone to whom private matters are told",
    "connive": "form intrigues (for) in an underhand manner",
    "cumulative": "increasing by successive addition",
    "debase": "corrupt morally or by intemperance or sensuality",
    "decry": "express strong disapproval of",
    "deferential": "showing courteous regard for people's feelings",
    "demure": "shy or modest, often in a playful or provocative way",
    "deride": "treat or speak of with contempt",
    "despot": "a cruel and oppressive dictator",
    "diligent": "quietly and steadily persevering in detail or exactness",
    "elated": "exultantly proud and joyful; in high spirits",
    "eloquent": "expressing yourself readily, clearly, effectively",
    "embezzle": "appropriate fraudulently to one's own use",
    "empathy": "understanding and entering into another's feelings",
    "enmity": "a state of deep-seated ill-will",
    "erudite": "having or showing profound knowledge",
    "extol": "praise, glorify, or honor",
    "fabricate": "make up something artificial or untrue",
    "flabbergasted": "as if struck dumb with astonishment and surprise",
    "forsake": "leave someone who needs or counts on you; leave in the lurch",
    "furtive": "secret and sly",
    "gluttony": "habitual eating to excess",
    "gratuitous": "unnecessary and unwarranted",
    "haughty": "having or showing arrogant superiority",
    "hypocrisy": "pretending to have qualities or beliefs that you do not have",
    "impeccable": "without fault or error",
    "impertinent": "improperly forward or bold",
    "impudent": "improperly forward or bold",
    "indolent": "disinclined to work or exertion",
    "inept": "generally incompetent and ineffectual",
    "infamy": "a state of extreme dishonor",
    "inhibit": "limit the range or extent of",
    "innate": "present at birth but not necessarily hereditary",
    "insatiable": "impossible to fulfill, appease, or gratify",
    "insular": "narrowly restricted in outlook or scope",
    "intrepid": "invulnerable to fear or intimidation",
}

【讨论】:

    【解决方案2】:

    很高兴回答您的问题。希望对你有所帮助。

    from bs4 import BeautifulSoup
    import requests
    URL = "https://www.vocabulary.com/lists/176046"
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, 'html.parser')
    corrupted = soup.find_all(attrs={'class': 'word dynamictext'})
    corrupted_2 = soup.find_all(attrs={'class': 'definition'})
    
    my_dict = {}
    for i in range(len(corrupted)):
        words = corrupted[i].text
        definitions = corrupted_2[i].text
        my_dict[words] = definitions
    
    print(my_dict)
    

    【讨论】:

      猜你喜欢
      • 2022-10-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2017-04-30
      • 2018-12-04
      相关资源
      最近更新 更多