Python Forum

Full Version: please help me remove error for string.strip()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#!/usr/bin/python3

from bs4 import BeautifulSoup
import requests
import lxml
import csv


URL = "https://sandiego.craigslist.org/search/sof"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "lxml")

# h3 tag
job_heading = []
for a in soup.find_all("h3"):
    job_heading.append(a.string.strip())

# location
time = []
for b in soup.find_all("time"):
    time.append(b.string.strip())

with open('output.csv', 'w') as file:
    writer = csv.writer(file, delimiter=',')

    writer.writerow(["job_heading", "time"])

    for i in range(25):
        writer.writerow([
            job_heading[i],
            time[i]
        ])
please, help me remove this error so after running this script, there is no "AtributeError: 'NoneType' object has no attribute 'strip'" thanks.
try a.strip() not a.string.strip()
thanks..this helped

link
Look at a in:

for a in soup.find_all("h3"):
    job_heading.append(a.string.strip())
If you look at type(a) you get:

Quote:>>> type(a)
<class 'bs4.element.Tag'>

Dunno what that is, but is not a string and probably that's why you can't .strip() it: no string.

snippsat is the guy to ask with bs4 but I got this from here and saved it:

# Can be a list of tags
# tags = ['h3']
# for tags in soup.find_all(tags):
for tags in soup.find_all('h3'):
    print(tags.text.strip())

# or like this
for tags in soup.find_all('span'):
    print(tags.text.strip())