Python Forum
please help me remove error for string.strip()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help me remove error for string.strip()
#1
Smile 
#!/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.
Reply
#2
try a.strip() not a.string.strip()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
thanks..this helped

link
Reply
#4
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 202 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  extract only text strip byte array Pir8Radio 7 2,789 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Remove a space between a string and variable in print sie 5 1,706 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  How do I remove spurious "." from a string? Zuhan 7 1,962 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy
  strip() pprod 8 3,372 Feb-16-2021, 01:11 PM
Last Post: buran
  How to remove char from string?? ridgerunnersjw 2 2,484 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  Remove from end of string up to and including some character lbtdne 2 2,285 May-17-2020, 09:24 AM
Last Post: menator01
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,420 May-15-2020, 01:37 PM
Last Post: snippsat
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 2,701 Apr-08-2020, 07:18 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020