【问题标题】:Indeed scraper bs4, splitting parsed HTML code after grabbing it确实是scraper bs4,抓取后拆分解析的HTML代码
【发布时间】:2021-10-29 18:14:31
【问题描述】:
import pandas as pd
from bs4 import BeautifulSoup
import requests
import os
url = 'https://fr.indeed.com/jobs?q=data%20anlayst&l=france'

#grabbing page content and parsing it into html
def data_grabber(url):
    
    page = requests.get(url)
    html = page.text
    soup = BeautifulSoup(html, 'html.parser')
    job_soup = soup.find_all('div', {"class":"job_seen_beacon"})
    return job_soup


def job_title(url):
    titles = data_grabber(url)   
    for title in titles:
        t = title.find_all('tbody')
        return t

这是我的源代码,我在 jupyter notebook 中对其进行了测试,以确保我的功能正常工作,但我遇到了一个小障碍。我的第一个函数中的 html 汤完美运行。它确实获取了所有信息,尤其是 job_seen_beacon 类。

Mr job_title 函数是错误的,因为它只输出它找到的第一个 'tbody' 类。 refer to image here, I don't have enough points on stack

而对于我的 data_grabber,它会返回每个 job_seen_beacon。 If you were able to scroll, you would easily see the multiple job_seen_beacon's.

我显然遗漏了一些东西,但我看不到它,有什么想法吗?

【问题讨论】:

    标签: python function web-scraping beautifulsoup


    【解决方案1】:

    会发生什么?

    在你成为return 的那一刻,你离开了function,这发生在第一次迭代中。

    不确定你的代码最终会在哪里结束,但你可以这样做:

    def job_title(item):
        title = item.select_one('h2')
        return title.get_text('|',strip=True).split('|')[-1] if title else 'No Title'
    

    示例

    from bs4 import BeautifulSoup
    import requests
    
    url = 'https://fr.indeed.com/jobs?q=data%20anlayst&l=france'
    
    #grabbing page content and parsing it into html
    def data_grabber(url):
        
        page = requests.get(url)
        html = page.text
        soup = BeautifulSoup(html, 'html.parser')
        job_soup = soup.find_all('div', {"class":"job_seen_beacon"})
        return job_soup
    
    
    def job_title(item):
        title = item.select_one('h2')
        return title.get_text('|',strip=True).split('|')[-1] if title else 'No Title'
    
    def job_location(item):
        location = item.select_one('div.companyLocation')
        return location.get_text(strip=True) if location else 'No Location'
    
    data = []
    
    for item in data_grabber(url):
        data.append({
            'title':job_title(item),
            'companyLocation':job_location(item)
        })
    
    data
    

    输出

    [{'title': 'Chef de Projet Big Data H/F', 'companyLocation': 'Lyon (69)'},{'title': 'Chef de Projet Big Data F/H', 'companyLocation': 'Lyon 9e (69)'}]
    

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2021-07-08
      • 2017-12-19
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多