【发布时间】:2018-03-18 00:11:06
【问题描述】:
HTML:
<div class="job-result-logo-title">
<div class="job-result-logo">
<a href="/Recruiters/SQS-Ireland-5673.aspx"><img alt="SQS Ireland" src="/Logos/SQS-Ireland-small-5673.gif"></a>
</div>
<div class="job-result-title">
<h2 itemprop="title"><a href="/Jobs/QA-Analyst-8148774.aspx">QA Analyst</a>
</h2>
<h3 itemprop="name">
<a itemprop="hiringOrganization" itemscope="" itemtype="https://schema.org/Organization" href="/Recruiters/SQS-Ireland-5673.aspx">SQS Ireland</a>
</h3>
</div>
</div>
<div class="job-result-overview" style="display: ">
<ul class="job-overview">
<li itemprop="baseSalary" class="salary">Negotiable</li>
<li itemprop="datePosted" class="updated-time">Updated 17/03/2018</li>
<li itemprop="jobLocation" class="location">
<a href="/Jobs/Dublin-City-Centre/">Dublin City Centre</a>
<span> /</span> <a href="/Jobs/Dublin-South/">Dublin South</a>
<span> /</span> <a href="/Jobs/Dublin-North/">Dublin North</a>
</li>
</ul>
</div>
我的代码:
def find_data(source):
for a in source.find_all('div', class_='job-result-title'):
job_info = a.find('h2').find('a')
company_name = a.find('h3').find('a').get_text()
url = job_info['href']
full_url = base_url + url
role = job_info.get_text()
for ul in source.find_all('ul', class_='job-overview'):
date = ul.find('li',class_='updated-time').get_text().replace('Updated','').strip()
append_data("data.csv", company_name, role, full_url, date)
我已经尝试了太多替代此代码的方法,还尝试在这里寻找类似的答案,但没有运气,我总是从这行代码中得到相同的日期,我不确定为什么它没有迭代所有相同的标签都包含每个标签的日期:
<li itemprop="datePosted" class="updated-time">Updated 17/03/2018</li>
【问题讨论】:
-
您仅在循环之后附加数据,您必须将添加(append_data 函数)添加到循环内的 csv
-
<div class="job-result-logo-title">的父级是什么? irishjobs.ie 上的哪个页面? -
嗨@TigerTV.ru,我从这里得到了一切:irishjobs.ie/…
-
您需要一个循环,
for a in source.find_all('div', class_='module-content'):在循环内查找信息并为每次迭代附加数据。 -
感谢@TigerTV.ru,您的解决方案成功了,我没有从父 div 迭代,这是主要问题。
标签: python python-3.x beautifulsoup tags