Beautiful Soup Issues, struggling to get address and times

Viewed 15

I am new to python and beautiful soup.. I have been struggling to find this solution.

I have to loop through html of addresses, name of the place where the address is, and the date and locations.

At this point, all I am getting is the name of the place.

This is the code I currently have :


soup = BeautifulSoup(open(r"/Users/braeapley/Documents/The Peach Truck Tour Pre-Orders.html"),"html.parser")

for item in soup.find_all(attrs={"data-id": True}):
    name = item.find("h3").get_text()
    address = item.find("address--js")

print (item['data-id'])
    print(name)
    print(address).get_text

#general= soup.find_all(class_ = "no-bullet tour-list")

#name = general.find("h3")[0].get_text()

#location = name.find_all("h3")[0].get_text()

#name = general.find_all("h3")[0].get_text()

When I run this, I get this error "AttributeError: 'NoneType' object has no attribute 'get_text'"

Here is the html code I am trying to work with, haven't moved to the times yet.. But I am struggling to pull the address because it is in svg.

</div>
</li>
<li id="tour-7" data-id="7" class="tour tmphide hide" data-category="Tour">
   <div class="box">
      <h3><b>Rural King</b></h3>
      <div class="distance"></div>
      <div class="meta">
         <div class="address--js">
            <svg width="11" height="16" viewBox="0 0 11 16" fill="none" xmlns="http://www.w3.org/2000/svg">
               <path d="M5.33333 0C2.53467 0 0 2.26867 0 5.068C0 7.86667 2.31267 11.208 5.33333 16C8.354 11.208 10.6667 7.86667 10.6667 5.068C10.6667 2.26867 8.13267 0 5.33333 0ZM5.33333 7.33333C4.22867 7.33333 3.33333 6.438 3.33333 5.33333C3.33333 4.22867 4.22867 3.33333 5.33333 3.33333C6.438 3.33333 7.33333 4.22867 7.33333 5.33333C7.33333 6.438 6.438 7.33333 5.33333 7.33333Z" fill="#e2231a"></path>
            </svg>
            1650 S Main St, Madisonville, KY 42431, USA
         </div>
      </div>
      <div class="toggle-info">
         <span class="select-pickup-date-label">Select pick-up date:</span>
         <ul class="tour_date_select">
            <li class=""><label class="date-1"><span>Select pick-up date:</span> <input type="radio" name="attributes[Location Date/Time]" value="06/29/2021 | 01:00 PM - 03:00 PM ET"><input type="radio" name="attributes[tour-date-id]" value="1440">06/29/2021 | 01:00 PM - 03:00 PM ET<span class="checkmark"></span></label></li>
            <li class=""><label class="date-2"><span>Select pick-up date:</span> <input type="radio" name="attributes[Location Date/Time]" value="08/04/2021 | 01:00 PM - 03:00 PM ET"><input type="radio" name="attributes[tour-date-id]" value="1465">08/04/2021 | 01:00 PM - 03:00 PM ET<span class="checkmark"></span></label></li>
            <li class="hide"><input type="hidden" class="selected-date" name="attributes[selected-date]" value=""></li>
         </ul>
1 Answers

Call .get_text() on the tag to get the address (html_doc is your HTML snippet from the question):

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, "html.parser")

for item in soup.find_all(attrs={"data-id": True}):
    name = item.find("h3").get_text()
    address = item.find(class_="address--js")  # <-- use class_="..."

    print(item["data-id"])
    print(name)
    print(address.get_text(strip=True))        # <-- call .get_text(strip=True)

Prints:

7
Rural King
1650 S Main St, Madisonville, KY 42431, USA
Related