简体   繁体   中英

NoneType object has no attribute find_all error using beautiful Soup

I am trying to read the following:

在此处输入图像描述

My goal is to read every job title from this page - https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python

What I've tried:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python'
page = requests.get(URL).text
soup = BeautifulSoup(page, 'html.parser')
results = soup.find(id='ResultsContainer')
 
# Look for Python jobs
python_jobs = results.find_all("div", string=lambda t: "python" in t.lower())

for p_job in python_jobs:
    link = p_job.find("h3")["href"]
    print(p_job.text.strip())
    print(f"Apply here: {link}\n")

But I get the following error:

AttributeError: 'NoneType' object has no attribute 'find_all'

How can I read all the titles?

Problem is, there isn't any tag with id="ResultsContainer" . You can search all <h3> tags with text Python and then find parent <a> tag for an url:

import requests
from bs4 import BeautifulSoup


URL = 'https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python'
page = requests.get(URL).text
soup = BeautifulSoup(page, 'html.parser')

results = soup.find_all('h3', text=lambda t: 'python' in t.lower())
for r in results:
    print(r.text)
    print(r.find_parent('a')['href'])
    print('-' * 80)

Prints:

Senior Python Developer
https://www.cvbankas.lt/senior-python-developer-vilniuje/1-6719819
--------------------------------------------------------------------------------
Full Stack Engineer (React + Python)
https://www.cvbankas.lt/full-stack-engineer-react-python-vilniuje/1-6665723
--------------------------------------------------------------------------------
Python programuotojas (Mid-Senior)
https://www.cvbankas.lt/python-programuotojas-mid-senior-vilniuje/1-6693547
--------------------------------------------------------------------------------
Python Developer
https://www.cvbankas.lt/python-developer-vilniuje/1-6604883
--------------------------------------------------------------------------------

Your problem is there is no element has a id "ResultsContainer" .

But refer to the struct of the page, You could use css selector to get all the information directly:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python'
page = requests.get(URL).text
soup = BeautifulSoup(page, 'html.parser')
results = soup.select("div.list_cell > .list_h3")
for i in results:
    print(i.text)

Result:

Data Engineer
Data Analyst
VYRESNYSIS INŽINIERIUS STRATEGIJOS IR TYRIMŲ SKYRIUJE
Senior Python Developer
Full Stack Engineer (React + Python)
DevOps Engineer
Linux Systems Automation Engineer
Big Data Developer
Big Data Devops Engineer
Python programuotojas (Mid-Senior)
DATA SCIENTIST
DEVOPS INŽINIERIAUS (e-commerce platformos produktų optimizavimas užsienio rinkoms)
LINUX Sistemų administratorius (-ė)
QA engineer
Blockchain Developer
Backend Software Engineer
FW/HW Quality Assurance Engineer
Software developer in Test
Python Developer
Senior Backend Engineer

Checkout my codes:

import requests
from bs4 import BeautifulSoup
URL = 'https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python'
page = requests.get(URL).text
soup = BeautifulSoup(page, 'html.parser')
h3_tags = soup.findAll("h3", {"class": "list_h3"})
for x in h3_tags:
    if "Python" in x.text:
        print(x.text)
        print(x.find_parent('a')['href'])
        print()

And The output are:

Senior Python Developer
https://www.cvbankas.lt/senior-python-developer-vilniuje/1-6719819

Full Stack Engineer (React + Python)
https://www.cvbankas.lt/full-stack-engineer-react-python-vilniuje/1-6665723

Python programuotojas (Mid-Senior)
https://www.cvbankas.lt/python-programuotojas-mid-senior-vilniuje/1-6693547

Python Developer
https://www.cvbankas.lt/python-developer-vilniuje/1-6604883

Replace your code with below code:

import requests
from lxml import etree
from bs4 import BeautifulSoup

URL = 'https://www.cvbankas.lt/?miestas=Vilnius&padalinys%5B%5D=&keyw=python'
page = requests.get(URL).text
soup = BeautifulSoup(page, 'html.parser')

dom = etree.HTML(str(soup))
elements = dom.xpath('//h3[@class="list_h3"]')
for element in elements:
  print(element.text)

This method: soup.find(id='ResultsContainer') found no element matching the criteria, and therefore returned None .

In this line: python_jobs = results.find_all("div", string=lambda t: "python" in t.lower()) , the value of results is None .

None.find_all does not exist. ( AttributeError: 'NoneType' object has no attribute 'find_all' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM