[[SOLVED] Python Bs4不能在双倍跨度内包含纯文本该死文本

问题描述 投票:0回答:1

伙计们,所以我想刮掉Indeed网站,到目前为止,除薪水外,其他所有东西都运转良好(当然,笑了。)>

所以我离请求,requests_html和bs4的专家还很远因此,经过一个小时的无处不在的查找,我找不到我的特定问题的答案,所以我在这里...

我已经缩短了代码,使事情变得更容易(我将抓取更多的内容)但是我以.text为例,它可以正常工作,但是只有一个范围,而不是薪水范围中的一个范围:

<div class="salarySnippet salarySnippetDemphasizeholisticSalary">
<span class="salary no-wrap">
<span class="salaryText">
1 900 € - 2 100 € par mois</span>
</span>
</div>

我是法语,所以如果您想尝试去巴黎或在输入中尝试,确实是法语版本:

from bs4 import BeautifulSoup
import requests as req

print("_____Indeed Job Scaper_____")
city = str(input("Enter your city name here: "))

url = ("https://www.indeed.fr/emplois?l=" + city)
u_req = req.get(url)
soup = BeautifulSoup(u_req.content, 'html.parser')
job_elems = soup.find_all('div' , class_='jobsearch-SerpJobCard') 

for job_elem in job_elems:
    compagny_name = job_elem.find('span' , class_="company")
    salary = job_elem.find("span", {"class": "salaryText"})

    print(compagny_name.text)
    print(salary.text)

如果我不要求在工资上打印.text,我会得到:

<span class="salaryText">
1 800 € - 4 000 € par mois</span>

[伙计们,所以我正试图刮掉Indeed网站,到目前为止,除薪水外,其他一切都运转良好(当然,笑)。所以我离请求,requests_html和bs4的专家还很远...

python beautifulsoup
1个回答
0
投票

似乎每个卡都不存在salaryText跨度。因此,在循环中,您需要先检查跨度是否存在,然后才能访问其内部文本。

© www.soinside.com 2019 - 2024. All rights reserved.