Maison  >  Article  >  interface Web  >  Comment créer un grattoir Crunchbase avec une démo de code

Comment créer un grattoir Crunchbase avec une démo de code

Barbara Streisand
Barbara Streisandoriginal
2024-10-18 12:58:30564parcourir

データに金の価値がある時代において、Crunchbase は宝の山です。ここには、何千もの企業プロフィール、投資データ、経営陣の地位、資金調達情報、ニュースなどが掲載されています。 Crunchbase スクレイピングを使用すると、金の塊 (必要な洞察) を取得し、すべての破片 (自分に無関係なその他すべての情報) を取り除くことができます。

この記事では、すべての技術的な詳細と Python を使用したコードを含め、Crunchbase スクレイパーをゼロから構築するプロセスを、実際に実行できるデモとともに説明します。そうは言っても、Crunchbase スクレーパーの構築は時間のかかる作業であり、途中で多くの課題があることも理解する必要があります。そのため、代わりに作業を行う有料の API ベース ツールである Proxycurl を使用した別のアプローチのデモも行います。両方のオプションが用意されているので、それぞれの利点を比較検討し、ニーズに最も適したものを選択できます。

ここでは、Python を使用して Web サイトから会社名と本社都市を抽出する基本的な Crunchbase スクレーパーのスニーク ピークを示します。

import requests
from bs4 import BeautifulSoup

url = 'https://www.crunchbase.com/organization/apple'
headers = {'User-Agent': 'Mozilla/5.0'}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')

name_section = soup.find('h1', class_='profile-name')
company_name = name_section.get_text(strip=True) if name_section else 'N/A'

headquarters_section = soup.find('span', class_='component--field-formatter field_type_text')
headquarters_city = headquarters_section.get_text(strip=True) if headquarters_section else 'N/A'

print(f"Company Name: {company_name}")
print(f"Headquarters City: {headquarters_city}")

次に、代替アプローチである Proxycurl について説明します。これは比較的効率的な Crunchbase スクレイピング ツールであり、わずか数行のコードを使用して同じ企業情報を取得できます。ここでの追加の利点は、HTML 解析や Proxycurl によるスクレイピングの障害について心配する必要がないことです。

import requests

api_key = 'YOUR_API_KEY'
headers = {'Authorization': 'Bearer ' + api_key}
api_endpoint = 'https://nubela.co/proxycurl/api/linkedin/company'
params = {
    'url': 'https://www.linkedin.com/company/apple/',
    }

response = requests.get(api_endpoint, params=params, headers=headers)
data = response.json()

print(f"Company Name: {data['company_name']}")
print(f"Company Headquarter: {data['hq']['city']}")

この記事を読み終えるまでに、両方の方法に慣れ、情報に基づいた意思決定ができ​​るようになります。したがって、腕まくりして独自のスクレイパーをコーディングすることに興奮している場合でも、ワンストップ ソリューションを求めている場合でも、読み続けて Crunchbase スクレイパーをセットアップしてください。

Crunchbase Scraper をゼロから構築する

Crunchbase には、買収、人物、イベント、ハブ、資金調達ラウンドなど、いくつかのデータ タイプが含まれています。この記事では、会社の説明を解析して JSON データとして取得するための単純な Crunchbase スクレイパーの構築について説明します。 Apple を例にしてみましょう。

まず、会社の説明を抽出する関数を定義する必要があります。 get_company_description() 関数は、会社の説明を含む Span HTML 要素を検索します。次に、テキストを抽出して返します。

def get_company_description(raw_html):
    description_section = raw_html.find("span", {"class": "description"})
    return description_section.get_text(strip=True) if description_section else "Description not found"

これにより、スクレイピングする会社プロファイルの URL (この場合は Apple のプロファイル) に HTTP GET リクエストが送信されます。完全なコードは次のようになります:

import requests
from bs4 import BeautifulSoup

def get_company_description(raw_html):
    # Locate the description section in the HTML
    description_section = raw_html.find("span", {"class": "description"})

    # Return the text if found, else return a default message
    return description_section.get_text(strip=True) if description_section else "Description not found"

# URL of the Crunchbase profile to scrape
url = "https://www.crunchbase.com/organization/apple"
# Set the User-Agent header to simulate a browser request
headers = {"User-Agent": "Mozilla/5.0"}

# Send a GET request to the specified URL
response = requests.get(url, headers=headers)

# Check if the request was successful (status code 200)
if response.status_code == 200:
    # Parse the HTML content of the response using BeautifulSoup
    soup = BeautifulSoup(response.content, "html.parser")

    # Call the function to get the company description
    company_description = get_company_description(soup)

    # Print the retrieved company description
    print(f"Company Description: {company_description}")
else:
    # Print an error message if the request failed
    print(f"Failed to retrieve data. Status Code: {response.status_code}")

このスクリプトは、Crunchbase から Apple の会社説明を取得するためのトリックを実行します。あなたの経験と何を探しているかによっては、物事はさらに複雑になる可能性があります。大量のデータの処理、ページネーションの管理、authwall メカニズムのバイパスなど、その過程には多くのハードルがあります。以下のことを行う必要があることに注意してください。

  • 関心のあるフィールドごとにこのアクションを実行します。
  • Web ページの変更を常に最新の状態に保ってください。 Web サイトでのフィールドの表示方法がわずかに変更されただけでも、スクレイピング ロジックに軽微または大幅な調整が加えられる可能性があります。

**注: Web サイトの利用規約と robots.txt ファイルをチェックして、法的制限内で責任を持ってスクレイピングしていることを確認してください。

Crunchbase Scraper の構築が難しいのはなぜですか?

独自の Crunchbase スクレーパーを構築することは実行可能な選択肢ですが、Gung-ho に行く前に、どのような課題が待ち構えているかを知っておいてください。

正確さと完全性

抽出されたデータが虚偽であれば、あなたの努力は無意味になります。手動でスクレイピングするとエラーの範囲が大きくなり、ページが完全に読み込まれない場合、または一部のコンテンツが iframe または外部リソースに埋め込まれている場合、コードによって重要なデータが見落とされる可能性があります。

Crunchbaseの構造と変更点

ウェブページの HTML を解析して特定のデータフィールドを抽出することは、スクレイピングの基本的な手順です。 Crunchbase の HTML は複雑で、動的要素と複数のコンテナ層が含まれています。適切なデータを特定して対象とすること自体がタスクです。これに、Web サイトの構造の変化が加わると、仕事が 10 倍難しくなる可能性があります。

authwall とアンチスクレイピングメカニズムの処理

Crunchbase は、ほとんどのデータを認証ウォールの背後で保護しており、ログイン資格情報またはプレミアム アカウントが必要です。スクレーパーでログイン セッション、トークン、または Cookie を手動で処理すると、特に複数のリクエストにわたってこれらのセッションを維持する場合、タスクがより複雑になります。同様に、Crunchbase はボット検出システムを使用し、リクエストのレート制限を行います。ブロックされるリスクがあり、これらの保護をバイパスするには、プロキシのローテーションや CAPTCHA の処理などの手法を実装する必要がありますが、言うは易く行うは難しです。

独自の Crunchbase スクレーパーを構築すると、柔軟性と達成感が得られますが、それに伴う課題と比較検討してください。必要なデータを取得するには、深い技術的専門知識、継続的な監視、努力が必要です。このプロセスがいかに時間がかかり、エラーが発生しやすいかは言うまでもありません。労力とメンテナンスがニーズにとって本当に価値があるかどうかを検討してください。

Crunchbase Scraper をセットアップする手間のかからない方法

ふぅ! Crunchbase Scraper をゼロから構築するのは、確かにかなりの大変な作業です。多くの時間と労力を費やすだけでなく、潜在的な課題にも常に目を光らせておく必要があります。 Proxycurl の存在に感謝します!

Proxycurl のエンドポイントを利用して、必要なすべてのデータを JSON 形式で取得します。また、Crunchbase は会社で利用可能な公開データのみを提供するため、手の届かないデータはありません。個人情報をスクレイピングしようとすると、結果は 404 になります。エラー コードを返すリクエストに対して料金が請求されることはありませんので、ご安心ください。

Proxycurl は、会社プロファイル エンドポイントの下に標準フィールドのリストを提供します。レスポンスの完全な例は、レスポンスを生成したリクエストの下の右側にあるドキュメントで確認できます。 Proxycurl には、リクエストに応じて次のフィールドをスクレイピングする機能があります:

  • カテゴリー
  • 資金調達データ
  • 出口データ
  • 買収
  • 追加

リクエストするこれらの各フィールドには追加のクレジットコストがかかるため、必要なパラメータのみを選択してください。ただし、必要な場合は、Proxycurl によって 1 つのパラメータが保管されます!

Proxycurl について理解したところで、実際に動作するデモを見てみましょう。 Postman の例と Python の例の 2 つを含めます。

Postman 経由で Proxycurl を使用した Crunchbase スクレイピング

ステップ 1: アカウントを設定し、API キーを取得する

Proxycurl でアカウントを作成すると、一意の API キーが割り当てられます。 Proxycurl は有料 API であり、ベアラー トークン (API キー) を使用してすべてのリクエストを認証する必要があります。仕事用メールでサインアップした場合は 100 クレジット、個人用メールを使用した場合は 10 クレジット も獲得できます。そうすれば、すぐに実験を始めることができます。ダッシュボードは次のようになります。

How To Build A Crunchbase Scraper In  With Code Demo

ここから、下にスクロールして、個人プロファイル エンドポイントまたは会社プロファイル エンドポイントを操作することを選択できます。個人プロフィール エンドポイントは、LinkedIn をスクレイピングしたい場合に便利なツールです。詳細については、「LinkedIn データ スクレイパーの構築方法」をご覧ください。

この使用例では、会社プロファイル エンドポイントだけを操作します。

ステップ 2: Postman を実行し、ベアラー トークンを設定します。

Postman で Proxycurl のコレクションに移動し、Company Profile Endpoint ドキュメントをクリックして、「Run in Postman」というオレンジ色のボタンを見つけてクリックします。次に、「フォークコレクション」をクリックし、好きなようにログインします。このように見えるはずです。 Postman で Proxycurl API をセットアップする方法に関する完全なチュートリアルがあります。

How To Build A Crunchbase Scraper In  With Code Demo
Postman での Proxycurl API のセットアップ

Postman にアクセスしたら、[認証] に移動し、[ベアラー トークン] を選択してトークン (API キー) を追加し、それを Proxycurl に制限します。これは、「変数」タブから行うか、「トークン」フィールドに入力を開始したときに表示されるポップアップから行うことができます。トークンに好きな名前を付けるか、単に「Bearer Token」という名前を付けます。

認証タイプが「ベアラー トークン」に設定されていること、および [トークン] フィールドに「{{ベアラー トークン}}」と入力していることを確認して、右上隅にある [保存] をクリックします。 必ず [保存] をクリックしてください!! ページは次のようになります:

How To Build A Crunchbase Scraper In  With Code Demo

ステップ 3: ワークスペースに移動する

左側の [マイ ワークスペース] で、Proxycurl コレクションに移動し、次に Company API に移動します。ドロップダウン メニューにオプションのリストが表示されますが、次のことを知っておく必要があります:

  • Company Profile Endpoint: Enriches company profile with Crunchbase data like funding, acquisitions, etc. You will need to use the company’s LinkedIn profile URL as input parameter to the API.
  • Company Lookup Endpoint: Input a company’s website and get its LinkedIn URL.
  • Company Search Endpoint: Input various search parameters and find a list of companies that matches that search criteria, and then pull Crunchbase data for these companies.

How To Build A Crunchbase Scraper In  With Code Demo
The various company-related endpoints

Step 4: Edit your params and send!

Go to Company Profile Endpoint and from there, you can uncheck some of the fields if you want or modify others. For instance, you might want to change use_cache from if-present to if-recent to get the most up-to-date info, but maybe you don't need the acquisitions information this time.

How To Build A Crunchbase Scraper In  With Code Demo
Choose the relevant fields that you need. Some cost extra credits.

Once you've modified all the fields to your liking, click the blue "Send" button in the upper left-hand corner. Your output should look something like this.

How To Build A Crunchbase Scraper In  With Code Demo

If you come across a 401 status code, it is most likely you forgot to hit Save after setting the Authorization type to {{Bearer Token}} in Step 2. A good way to troubleshoot this is to see if you can fix it by editing the Authorization tab for this specific query to be the {{Bearer Token}} variable. If that fixes it, then the auth inheritance isn't working, which probably means you forgot to save.

Crunchbase scraping with Proxycurl via Python

Now let’s try and do the same with Python. In the Proxycurl docs under Company Profile Endpoint, you can toggle between shell and Python. We’ll use the company endpoint to pull Crunchbase-related data, and it’s as simple as switching to Python in the API docs.

How To Build A Crunchbase Scraper In  With Code Demo
Toggle between shell and Python

Now, we can paste in our API key where it says YOUR_API_KEY. Once we have everything set up, we can extract the JSON response and print it. Here’s the code for that, and you can make changes to it as needed:

import requests

api_key = 'YOUR_API_KEY'
headers = {'Authorization': 'Bearer ' + api_key}
api_endpoint = 'https://nubela.co/proxycurl/api/linkedin/company'
params = {
    'url': 'https://www.linkedin.com/company/apple/',
    'categories': 'include',
    'funding_data': 'include',
    'exit_data': 'include',
    'acquisitions': 'include',
    'extra': 'include',
    'use_cache': 'if-present',
    'fallback_to_cache': 'on-error',
}

response = requests.get(api_endpoint, params=params, headers=headers)
print(response.json())

Now, what you get is a structured JSON response that includes all the fields that you have specified. Something like this:

"linkedin_internal_id": "162479",
   "description": "We're a diverse collective of thinkers and doers, continually reimagining what's possible to help us all do what we love in new ways. And the same innovation that goes into our products also applies to our practices -- strengthening our commitment to leave the world better than we found it. This is where your work can make a difference in people's lives. Including your own.\n\nApple is an equal opportunity employer that is committed to inclusion and diversity. Visit apple.com/careers to learn more.",
   "website": "http://www.apple.com/careers",
   "industry": "Computers and Electronics Manufacturing",
   "company_size": [
       10001,
       null
   ],
   "company_size_on_linkedin": 166869,
   "hq": {
       "country": "US",
       "city": "Cupertino",
       "postal_code": "95014",
       "line_1": "1 Apple Park Way",
       "is_hq": true,
       "state": "California"
   },
   "company_type": "PUBLIC_COMPANY",
   "founded_year": 1976,
   "specialities": [
       "Innovative Product Development",
       "World-Class Operations",
       "Retail",
       "Telephone Support"
   ],
   "locations": [
       {
           "country": "US",
           "city": "Cupertino",
           "postal_code": "95014",
           "line_1": "1 Apple Park Way",
           "is_hq": true,
           "state": "California"
        }
                 ]
...... //Remaining Data
}

Great! Congratulations on your journey from zero to data!

Is any of this legal?

Yes, scraping Crunchbase is legal. The legality of scraping is based on different factors like the type of data, the website’s terms of service, data protection laws like GDPR, and much more. The idea is to scrape for publicly available data within these boundaries. Since Crunchbase only houses public data, it is absolutely legal to scrape by operating within the Crunchbase Terms of Service.

Final thoughts

A DIY Crunchbase scraper can be an exciting project and gives you full control over the data extraction process. But be mindful of the challenges that come with it. Facing a roadblock in each step can make scraping a time-consuming and often fragile process that requires technical expertise and constant maintenance.

Proxycurl provides a simpler and more reliable alternative. Follow along with the steps and you can access structured company data through an API without worrying about any roadblocks. Dedicate your time by focusing on using the data and leave the hard work and worry to Proxycurl!

We'd love to hear from you! If you build something cool with our API, let us know at hello@nubela.co! And if you found this guide useful, there's more where it came from - sign up for our newsletter!

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn