【问题标题】:Python, 'NoneType' object has no attribute 'get_text'Python,“NoneType”对象没有属性“get_text”
【发布时间】:2016-10-11 16:50:05
【问题描述】:

输出显示:

“NoneType”对象没有“get_text”属性

我该如何解决这个问题?

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)

【问题讨论】:

  • 你可以调试看看table_row.find_all("td")是否返回了你期望的数据
  • 好吧,看起来tds[0].find("td") 和/或tds[1].find("td") 返回None。你可能想检查一下。
  • 嗨@cricket_007,table_row.find_all("td") 的输出将找到我想要的所有数据,但是当我使用 tds = table_row.find_all("td") 和 title = tds [0].get_text().strip() 它只会显示 6 个数据。我该如何解决这个问题?
  • 您希望展示什么?看一下html。 td 元素是否超过六个?
  • @cricket_007 它现在可以工作,但输出不是我想要的。一个部门有3个工作的部分。我该如何解决?

标签: python beautifulsoup


【解决方案1】:

不知道你为什么要在这里找到tds 里面的tds:

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

将其替换为:

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

为我工作。

【讨论】:

  • 谢谢。这可行,但我现在的问题是它只会显示 1 个类别; 1 份工作。一些类别有超过 1 个工作。我该怎么办?
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多