0

We have an internal web page with lists of IP addresses. The IP addresses are enclosed as list items. I am still somewhat new to Python and was hoping for a bit of direction. I would like to end up with a text file that comma separates these IPs.

<li>192.168.1.1</li>

I am using a publicly available website for my question, but the HTML source is similar.

import requests
from bs4 import BeautifulSoup

URL = 'https://www.w3schools.com/html/tryit.asp?filename=tryhtml_lists_intro'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')
list_items = soup.find_all('li')
print(list_items)

[<li>Coffee</li>, <li>Tea</li>, <li>Milk</li>, <li>Coffee</li>, <li>Tea</li>, <li>Milk</li>]

How can I further parse the output from list_items into a list/text file such as the following:

Coffee, Tea, Milk, Coffee, Tea, Milk

Thank You!

2
  • print([e.text for e in list_items]) Commented Mar 9, 2021 at 20:54
  • Or if you just want a string: print(", ".join([e.text for e in list_items])) Commented Mar 9, 2021 at 20:56

1 Answer 1

1

You are simply adding the entire tags to your list, instead of their text content.

list_items = ", ".join([li.text for li in soup.find_all("li")])
print(list_items)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone for their suggestions! This helps a new python person like myself a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.