-3

The output says:

'NoneType' object has no attribute 'get_text'

How can I fix this?

response = requests.get("https://www.exar.com/careers")

soup = BeautifulSoup(response.text, "html.parser")

data = []

table_main = soup.find_all("table", class_="table")
#pprint(table_main)

for table_row in table_main:
    job_category = table_row.find("th", class_="t3th").get_text().strip()
    tds = table_row.find_all("td")
    title = tds[0].find("td").get_text().strip()
    location = tds[1].find("td").get_text().strip()

    job = {
        "job_location": location,
        "job_title": title,
        "job_dept": job_category
    }
    data.append(job)

pprint(data)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    You could debug to see if `table_row.find_all("td")` returned the data you expect – OneCricketeer Oct 11 '16 at 16:51
  • Well, looks like `tds[0].find("td")` and/or `tds[1].find("td")` returns `None`. You might want to check that. – Biffen Oct 11 '16 at 16:52
  • Hi @cricket_007, table_row.find_all("td") the output of this will find all the data that I want but when I use tds = table_row.find_all("td") and title = tds[0].get_text().strip() it will only shows 6 data. How can I fix this? – Rich Win Monterola Oct 12 '16 at 06:54
  • What are you expecting to show? Look at the html. Are there more than six `td` elements? – OneCricketeer Oct 12 '16 at 12:32
  • @cricket_007 It's working now but the output is not what I wanted. The part where there are 3 jobs in a department. How can I fix that? – Rich Win Monterola Oct 12 '16 at 14:01
  • I have reverted your post because you've asked a new, unlreated, question, therefore invalidating the existing answer. Looking at the output you wanted, you need to the get one "category" from the `table_row`, and *then* loop over all positions and locations, collapsing all into a dictionary under one department. – OneCricketeer Oct 12 '16 at 16:24
  • @cricket_007, sorry I thought I can just ask the question here. I apologize. I'm new to this. – Rich Win Monterola Oct 12 '16 at 18:55
  • It's fine if you are new, just take the time to go over the [Help Center](http://stackoverflow.com/help), specifically the section on asking. That will help you ask good questions, as well. – OneCricketeer Oct 12 '16 at 20:00

1 Answers1

2

Not sure why are you trying to find tds inside tds here:

title = tds[0].find("td").get_text().strip()
location = tds[1].find("td").get_text().strip()

Replace it with just:

title = tds[0].get_text().strip()
location = tds[1].get_text().strip()

Works for me.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195