使用BeautifulSoup从网站上抓取其他网址的网站和内容

时间:2017-08-28 12:27:24

标签: python-2.7 web-scraping beautifulsoup

上下文

我正在试图抓住公司网站(我已获得许可)并从他们的工作部分提取工作职位。

所有职位都在第一页(目录)中列出了高级别详细信息,但这些职位的详细信息位于唯一网址上。我想让我的scraper能够识别目录中的相关位置,然后抓取唯一的URL。

到目前为止我编写的代码

import requests
from bs4 import BeautifulSoup 

url = "http://implementconsultinggroup.com/career/#/6257"
r = requests.get(url)

soup = BeautifulSoup(r.content)

links = soup.find_all("a")

for link in links:
     if "career" in link.get("href") and 'COPENHAGEN' in link.text:
             print "<a href='%s'>%s</a>" %(link.get("href"), link.text)

这给了我以下输出:

View Position

</a>
<a href='/career/business-analyst-within-human-capital-management/'>
Business analyst within human capital management
COPENHAGEN • We are looking for an ambitious student with an interest in HR 
who is passionate about working in the cross-field of people management, 
business and technology




View Position

</a>
<a href='/career/management-consultants-within-strategic-workforce-planning/'>
Management consultants within strategic workforce planning
COPENHAGEN • We are looking for consultants with profound experience from 
other consultancies

我希望刮刀从上面的输出中抓取所有内容。因此,请浏览URL并提取这些页面的内容。

如果有人知道如何做到这一点会很棒,或者可以指导我如何为我的刮刀创建此功能。

预期输出

我希望刮刀访问上面的网址,这会将我带到实际的工作发布页面(http://implementconsultinggroup.com/career/management-consultants-within-strategic-workforce-planning/),然后从该页面中提取详细信息。

战略人力规划中的管理顾问 在实施咨询集团,我们希望在咨询行业中发挥作用,因为我们相信,创造变革的能力是在日益全球化和动荡的世界中取得成功的先决条件。 我们经历了大量增加的激动人心的项目,因此我们正在寻找有能力的同事,他们具有与管理层合作的深厚知识,并希望与一群鼓舞人心的同事合作,为我们的客户创造真正的影响力。 工作内容 - 随着影响而变化 工作内容各不相同,但包括: 设计链接和支持业务战略的劳动力战略 实施战略人力规划 协助建立雄心勃勃且以数据为导向的劳动力规划方法 实施人才,职业和绩效管理计划 开发和优化人力资源流程 HRIS和HR数字化 提供与制定人力资源战略有关的咨询服务 协助开发团队和专业领域 我们的项目范围广泛,通常包括优化人力资源管理工具和流程,人员绩效管理解决方案以及卓越组织的建立。我们的使命之一是不断改善我们的领导行为,并成为最具吸引力的工作场所。 我们正在寻找那些人 至少持有一个相关的硕士学位 至少有3 - 5年的经验,最好是来自其他咨询公司 具有设计和实施战略和/或运营人力规划的经验 有能力和愿望将劳动力战略与业务战略联系起来 有能力在所有组织层面与客户建立良好的合作关系 能够解决问题并领导团队 您对创建结果充满热情,并且您相信在执行变更时采用感恩的方法。你可以是规范的,但是知道没有什么是黑白的。你喜欢多样性,你既有分析性又有共情性。在您未来的工作中,您希望担任顾问和项目经理的角色,并且您有兴趣担任顾问的几个方面。 我们提供什么? 我们提供具有吸引力的绩效相关薪酬方案,以及高素质和雄心勃勃的同事之间的良好发展机会。我们以高度自由,负责任和相互支持,努力不断努力,帮助客户创造卓越的成果。我们提供真正独特的文化,有用的同事网络以及试图改变咨询业务以创造影响变革的公司。  应用 有关该职位的更多信息,请联系Julius M. Opstrup,+ 45 2338 0004.有关招聘流程的更多信息,请联系Julie Palmqvist,+ 45 6124 4847。 申请截止日期是尽快。我们期待您的回音! 访问我们的职业页面,了解有关在实施咨询集团工作的更多信息,或了解有关哥本哈根办事处的更多信息。

2 个答案:

答案 0 :(得分:1)

import requests
from bs4 import BeautifulSoup 

url = "http://implementconsultinggroup.com/career/#/6257"
r = requests.get(url)
soup = BeautifulSoup(r.content)

position_block = soup.find('ul', class_="list-articles")
position_list = position_block.find_all('li')

position_links = []

for position in position_list:
    position_links.append(position.a['href'])


base_url = "http://implementconsultinggroup.com"

descriptions = []

for link in position_links:
    r = requests.get(base_url + link)
    soup = BeautifulSoup(r.content)

    descriptions.append(soup.find('div', class_='contentwithrelated').get_text())

答案 1 :(得分:0)

我认为没有别的东西可以刮掉。运行它,亲眼看看:

from elastalert.enhancements import BaseEnhancement, DropMatchException
import datetime
import time
import sys

def datetime_from_utc_to_local(utc_datetime):
    now_timestamp = time.time()
    offset = datetime.datetime.fromtimestamp(now_timestamp) - datetime.datetime.utcfromtimestamp(now_timestamp)
    return utc_datetime + offset

class DropFrom00To06(BaseEnhancement):
    def process(self, match):
        dateformat = "%Y-%m-%dT%H:%M:%S"
        exceptional_dateformat = "%Y-%m-%dT%H:%M:%SZ"
        timestamp = match['@timestamp'].split(".")[0]
        try:
            timestamp = datetime.datetime.strptime(timestamp, dateformat)
        except ValueError:
            timestamp = datetime.datetime.strptime(timestamp, exceptional_dateformat)
        except:
            print("Unexpected error:", sys.exc_info()[0])
            raise
        timestamp = datetime_from_utc_to_local(timestamp)
        timePart = timestamp.time()
        if timePart >= datetime.time(00, 00) and timePart <= datetime.time(8, 00):
            raise DropMatchException()