Home > Blockchain >  How to extract a particular Purpose and Name using BeautifulSoup?
How to extract a particular Purpose and Name using BeautifulSoup?

Time:10-26

I have written following code

    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)

and print gives following answer

[<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>]

I want to extract name and position earlier I was using indexing but it was dynamic could anyone advise how to extract name and purpose.

CodePudding user response:

I think you're looking for something like this:

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

Output (in this case):

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

CodePudding user response:

The problem with your code is you are searching the Tag element, but you want to search the text within the Tag:

for item in info_list: 
    if 'Name' in item.text or 'Purpose' in item.text:
        print(item)

<li>Name: Love and Sons</li>
<li>Purpose: Profit-focused maximized model for morph out-of-the-box technologies</li>

or print only the text from the item, replace the last line above with:

        print(item.text)

will return something like:

Name: Johnson, Miller and Woods
Purpose: Cloned content-based hierarchy for re-contextualize synergistic interfaces
  • Related