【问题标题】:How to extract a particular Purpose and Name using BeautifulSoup?如何使用 BeautifulSoup 提取特定目的和名称?
【发布时间】:2021-10-25 23:08:05
【问题描述】:

我写了以下代码

    page=requests.get("http://3.85.131.173:8000/random_company") 
    soup=BeautifulSoup(page.content,"html.parser")
    info_list=soup.find_all("li")
    print(info_list)

并打印给出以下答案

[<li>Name: Walker, Meyer and Allen</li>, <li>CEO: David Pollard</li>, <li>CTO: Sandra Boyd</li>, <li>Address: 275 Jones Station Suite 008
Bradburgh, UT 24369</li>, <li>Investment Round: C</li>, <li>Purpose: Reduced logistical contingency for whiteboard end-to-end applications</li>]

我想在之前使用索引提取名称和位置,但它是动态的,任何人都可以建议如何提取名称和用途。

反馈后我编辑的代码:

page=requests.get("http://3.85.131.173:8000/random_company") 
soup=BeautifulSoup(page.content,"html.parser")
info_list=soup.find_all("li")
print(info_list)
name=[]
purpose=[]

我现在可以成功打印姓名和位置。它给出以下输出 ['Name: Burnett and Sons'] 假设如果我只想要 Burnett and Sons 那么我应该怎么做?有什么建议吗?

【问题讨论】:

  • 遍历列表 (for item in info_list:) 并检查if "Name" in item:,同样为位置做
  • 你想提取所有名字和他们各自的位置吗?还是只有标记为 'Name' 的项目?
  • 你是“要提取姓名和职位”还是“要提取姓名和目的”?
  • 目前还不清楚你到底想刮什么。请edit您的问题以显示预期的输出。
  • @matiiss-我想提取名称和目的。我尝试了下面的代码 { page=requests.get("3.85.131.173:8000/random_company") soup=BeautifulSoup(page.content,"html.parser") info_list =soup.find_all("li") for item in info_list: if("Name" in item): print(item) } 我只得到一个目的输出。我需要名称和目的。我也循环了名称但它给了我输出作为目的?

标签: python html web-scraping beautifulsoup


【解决方案1】:

我认为您正在寻找这样的东西:

targets = ["Name","Purpose"]
for item in info_list:
    if item.text.split(":")[0] in targets:
        print(item.text)

输出(在这种情况下):

Name: Jimenez LLC
Purpose: Mandatory context-sensitive approach for leverage compelling communities

【讨论】:

    【解决方案2】:
    if 'Name' in item.text:
        name=name.append(item)        <-- Wrong: assigns None to name
    if 'Purpose' in item.text:
        purpose=purpose.append(item)  <-- Wrong: assign None to purpose
    

    上面两条指出的线是问题所在。 list.append() 返回无。
    (见进一步解释:Why does append() always return None in Python?

    要获得预期的输出,请删除 name= 部分并让 list.append() 内联添加到您的列表中,如下所示:

    for item in item_list: 
        if 'Name' in item.text:
            name.append(item.text)
        if 'Purpose' in item.text:
            purpose.append(item.text)
    
    print(name, purpose)
    

    应该打印:

    ['Name: Ward and Sons'] ['Purpose: User-friendly mission-critical algorithm for visualize killer e-business']
     
    

    【讨论】:

    • 我不想要位置我需要名称和用途。请查看我的代码。我无法在我的代码中提取名称和用途 info_list 变量。另外对于提取我不想使用索引.例如使用索引 info_list[0] 会给我名字,但我不会硬编码。
    • 知道了,谢谢解释,@cmpunk,请看我更新的代码
    • 您的代码的问题是只搜索 Tag 元素,而不是 Tag 中的 text
    • 谢谢你们的回复。我想要同样的东西但是当我运行完整的代码时{page=requests.get("3.85.131.173:8000/random_company") soup=BeautifulSoup(page.text," html.parser") item_list=soup.find_all("li") for item in info_list: if 'Name' in item.text or 'Purpose' in item.text: print(item) it is given me error in if statement error (NavigableString' 对象没有属性'text')
    • @cmpunk 看看它是如何运行的没有错误,按页面右上角的蓝色(>>> 运行)按钮:pythonanywhere.com/user/downshift/files/home/downshift/69715704/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多