BeautifulSoup Findall'list'对象没有属性'find_all'

时间:2016-10-04 14:58:30

标签: python-3.x beautifulsoup

from bs4 import BeautifulSoup
from pprint import pprint
import requests

url = "http://chk.tbe.taleo.net/chk01/ats/careers/searchResults.jsp?org=CDI&cws=1"

response = requests.get(url)

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

table_main = soup.select("table#cws-search-results")

table = table_main.find_all("tr")

for tr in table:
    job_title = tr.find_all("a")
    job_location = tr.find_all("b")

    job = {
         "job_title": job_title,
         "job_location": job_location
    }
    data.append(job)

pprint(jobs)

1 个答案:

答案 0 :(得分:4)

您收到错误,因为Tag返回Tag个对象列表(本例中为1个项目列表)而不是单个find_all对象,{{1 }}是Tag对象的方法,而不是Python list对象的方法。

变化:

table_main = soup.select("table#cws-search-results")

为:

table_main = soup.select_one("table#cws-search-results")

获取表示主表的Tag对象,然后在该对象上调用find_all将按预期工作。