使用BeautifulSoup获取特定的href

时间:2018-11-07 15:11:20

标签: html python-3.x web-scraping beautifulsoup python-requests

我正在尝试抓取express.static框中包含的所有链接。但是,我的模式不返回任何内容。我究竟做错了什么?如果我通常使用href = True查找“ a”,则无法获得所需的链接。

import requests
from bs4 import BeautifulSoup

url = 'https://www.nationalevacaturebank.nl/vacature/zoeken?query=&location=&distance=city&page=1&limit=100&sort=relevance&filters%5BcareerLevel%5D%5B%5D=Starter&filters%5BeducationLevel%5D%5B%5D=MBO'
page = requests.get(url)  
soup = BeautifulSoup(page.content, 'lxml')

ahrefs = soup.find_all('a', {'class': "article-link" , 'href': True})
for a in ahrefs:
    print(a.text)

1 个答案:

答案 0 :(得分:2)

这是一个有角度的网站,可以从外部Json API动态加载其内容。 api位于此处:https://www.nationalevacaturebank.nl/vacature/zoeken.json,需要设置cookie。以下将格式化您要提取的链接:

import requests

r = requests.get(
    'https://www.nationalevacaturebank.nl/vacature/zoeken.json',
    params = {
        'query': '',
        'location': '',
        'distance': 'city',
        'page': '1,110',
        'limit': 100,
        'sort': 'date',
        'filters[careerLevel][]': 'Starter',
        'filters[educationLevel][]': 'MBO'
    },
    headers = {
        'Cookie': 'policy=accepted'
    }
)

links = [
    "/vacature/{}/reisspecialist".format(t["id"])
    for t in r.json()['result']['jobs']
]

print(links)

Json结果还为您提供了嵌入在此页面中的所有卡元数据