如果找到某些字符串,则提取链接和文本 - BeautifulSoup

时间:2017-08-26 23:52:49

标签: python web-scraping beautifulsoup

我正在尝试运行beautifulSoup从网站中提取链接和文本(我已获得许可)

我运行以下代码来获取链接和文本:

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"):
             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




View Position

</a>
<a href='/career/management-consultants-within-supply-chain-strategy-
production-and-process-management/'>
Management consultants within supply chain strategy, production and process 
management
MALMÖ • We are looking for talented graduates who want a career in management 
consulting

这几乎是正确的,但是我只想在文本中有名称COPENHAGEN时返回位置(即不应该返回MALMO位置上方)。

该网站的HTML代码如下所示:

<div class="small-12 medium-9 columns top-lined">
                                    <a href="/career/management-consultants-within-supply-chain-management/" class="box-link">
                                    <h2 class="article__title--tiny" data-searchable-text="">Management consultants within supply chain management</h2>
                                    <p class="article__longDescription" data-searchable-text="">COPENHAGEN • We are looking for bright graduates with a passion for supply chain management and supply chain planning for our planning and execution excellence team.</p>
                                    <div class="styled-link styled-icon">
                                        <span class="icon icon-icon">
                                            <i class="fa fa-chevron-right"></i>
                                        </span>
                                        <span class="icon-text">View Position</span>
                                    </div>
                                </a>
                            </div>

1 个答案:

答案 0 :(得分:2)

似乎你可以添加另一个条件:

for(y in unique(dat$year)) {
    dat[[paste0("Year", y)]] <- (dat$Year==y)*1
}