#!/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.
please help me remove error for string.strip()
|
please help me remove error for string.strip()
|
|
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 Download my project scripts
Oct-14-2022, 07:48 AM
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) 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())
|
|
|
Users browsing this thread: 1 Guest(s)
