用BeautifulSoup在HTML标签中提取多个因子

2024-09-27 19:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从HTML文件中的每个重复标记中提取多个因子。你知道吗

。。。。 你知道吗

<div class="title">
    <a target="_blank" id="jl_fe575975c912af9e" href="https://www.indeed.com/company/Nestvestor/jobs/Data-Science-Intern-fe575975c912af9e?fccid=8eed076a625928e7&amp;vjs=3" onmousedown="return rclk(this,jobmap[0],0);" onclick=" setRefineByCookie(['radius']); return rclk(this,jobmap[0],true,0);" rel="noopener nofollow" title="Data Science Intern" class="jobtitle turnstileLink " data-tn-element="jobTitle">
        Data Science Intern</a>

    </div>

<div class="sjcl">
    <div>
<span class="company">
    Nestvestor</span>

</div>
<div class="jobsearch-SerpJobCard unifiedRow row result clickcard" id="p_9cfaca3374641aa0" data-jk="9cfaca3374641aa0" data-tn-component="organicJob">

<div class="title">
    <a target="_blank" id="jl_9cfaca3374641aa0" href="https://www.indeed.com/rc/clk?jk=9cfaca3374641aa0&amp;fccid=1779658d5b4ae2b0&amp;vjs=3" onmousedown="return rclk(this,jobmap[1],0);" onclick=" setRefineByCookie(['radius']); return rclk(this,jobmap[1],true,0);" rel="noopener nofollow" title="Product Manager" class="jobtitle turnstileLink " data-tn-element="jobTitle">
        Product Manager</a>

    </div>

<div class="sjcl">
    <div>
<span class="company">
    <a data-tn-element="companyName" class="turnstileLink" target="_blank" href="https://www.indeed.com/cmp/Sojern" onmousedown="this.href = appendParamsOnce(this.href, 'from=SERP&amp;campaignid=serp-linkcompanyname&amp;fromjk=9cfaca3374641aa0&amp;jcid=1779658d5b4ae2b0')" rel="noopener">
    Sojern</a></span>

。。。你知道吗

soup = BeautifulSoup(open(input("Enter a file to read: ")), "html.parser")


title = soup.find_all('div', class_='title')
for span in title:
    print(span.text)

company = soup.find_all('span', class_='company')
for span in company:
    print(span.text)

到目前为止,我已经知道如何得到以下结果:

工作标题1

作业标题2

作业标题3

在另一个代码结果中:

公司名称1

公司名称2

公司名称3

如何通过运行一次代码使结果看起来像这样:
工作标题1,公司名称1,
职位2,公司名称2,
职务3,公司名称3


Tags: div名称datareturntitle公司thiscompany
2条回答

从你所拥有的看来,你需要嵌套你的循环。没有网站,很难说,但我会尝试这样的东西。你知道吗

    company = soup.find_all('span', class_='company')
    title = soup.find_all('div', class_='title')
    for span in title:
        for x in company:
   print(x.text,span.text)

欢迎使用堆栈溢出,只需使用以下命令:

company = soup.find_all('span', class_='company')
title = soup.find_all('div', class_='title')
for t,c  in zip(title, company): 
print ("Job_Title :%s Company_Name :%s" %(t.text,c.text)) 

相关问题 更多 >

    热门问题