如何使用空类值从div中删除文本

时间:2018-04-28 15:28:07

标签: python python-3.x web-scraping beautifulsoup

您好如何在没有任何课程的情况下从div中删除文本?起初我尝试使用类'jobs page'从div中删除所有数据,然后没有类值,但它不起作用。

enter image description here

from bs4 import BeautifulSoup
import requests

a = {}


def antal_pl(name=''):
    try:
        page_response = requests.get('https://antal.pl/oferty-pracy?s=&sid=&did=Accountancy', timeout=40).text
        page_content = BeautifulSoup(page_response, 'lxml')
        data = page_content.find_all(class_ = 'jobs_page')
        data_in = data.find_all('div', class_ = None)
        print(data_in)

    except:
        ''
antal_pl( name='Accontancy')

2 个答案:

答案 0 :(得分:1)

如上所述,尝试使用以下方法从该网页中获取文本。我试图整理你的代码以使其看起来更干净。

from bs4 import BeautifulSoup
import requests

URL = "https://antal.pl/oferty-pracy?s=&sid=&did={}"

def antal_pl(name):
    res = requests.get(URL.format(name))
    soup = BeautifulSoup(res.text, 'lxml')
    data = soup.find(class_='header').find_next_sibling().text.strip()
    print(data)

if __name__ == '__main__':
    antal_pl("Accountancy")

结果:

Znaleziono 47 ofert pracy.

答案 1 :(得分:0)

使用XPATH

html = etree.HTML(wb_data)
html_data = html.xpath('/html/body/div/ul/li/a')

enter image description here