DEV Community

Cover image for How to scrape Bilibili videos by keyword for China market research with Python
Norbert Sobala
Norbert Sobala

Posted on

How to scrape Bilibili videos by keyword for China market research with Python

If you sell anything into China, or you just want to know what Chinese viewers actually watch in your niche, Bilibili (B站) is one of the better places to look. It has no official public search API for this kind of work, and the site is in Chinese, which puts a lot of people off.

This tutorial shows how to pull Bilibili search results into a table with the Bilibili Video Scraper on Apify. First without code, then with Python and pandas. Every number below comes from one real run I did today: the keyword 机械键盘 (mechanical keyboard), sorted by most views, 40 videos.

Bilibili search results for 机械键盘 in a table

What data you get

Each video becomes one row. These are the fields the actor returns:

Field What it contains
bvid, aid, url BV id, numeric id and link to the video
title, description, tags Video text, search highlighting stripped
coverUrl Cover image (HTTPS)
durationSec Length in seconds
publishedAt Upload time, ISO 8601, UTC
views, likes, danmaku, comments, favorites Engagement counts
coins, shares Filled for popular board, video URLs, or search with "Add full stats" on
category Bilibili section, e.g. 数码 (digital), 游戏 (games)
authorName, authorId, authorUrl Public channel of the uploader
source, searchQuery, rank How the row was found and its position
scrapedAt When the row was collected

Comment text and danmaku text are not collected, only their counts. That keeps private users out of the dataset.

What one run looked like

My input: one keyword, sortBy: "click" (most views), 40 results, full stats on. The run took 38 seconds and saved 40 videos.

A few things stood out once the data was in pandas:

  • Views ranged from 3.12M to 8.17M, median 4.72M. Together the 40 videos had about 189M views.
  • 30 of 40 sit in the 数码 (digital) category. The rest are scattered across daily life, esports, DIY, comedy and a couple of others.
  • 30 different channels made those 40 videos, so the top results are not owned by one or two creators.
  • 13 videos are shorter than 60 seconds. Mostly switch sound clips. Their median like rate (likes / views) was 0.94%, against 1.48% for longer videos. Favorites per view showed a bigger gap: 0.27% vs 0.82%.
  • Upload dates go from 2017 to 2026, and 16 of the 40 were posted in 2025 or later. Old videos keep ranking for years on this keyword.
  • The video with the most coins (a Bilibili tip currency) was a DIY build of a modular keyboard: 376,195 coins on 5.67M views.
  • Most common tags: 机械键盘 (37 videos), 键盘 (31), 外设 "peripherals" (25), 客制化 "custom" (20).

That last point is useful by itself. If you plan content or ads, the tag list tells you which words Chinese creators use for your product category.

Input options that matter

Input form of the Bilibili Video Scraper

  • mode: search (keywords), popular (the 热门 trending board) or videos (full stats for a list of video URLs or BV ids).
  • searchQueries: one or more keywords. Chinese works best for Chinese content, English brand names also work.
  • maxResultsPerQuery: how many videos per keyword. Bilibili search stops at roughly 1,000 per keyword (50 pages of 20).
  • sortBy: totalrank (best match), click (most views), pubdate (newest), dm (most danmaku), stow (most favorites).
  • includeDetails: adds coins, shares and exact likes to search rows. It makes one extra request per video, so the run is slower. Price stays the same.
  • proxyConfiguration: datacenter proxy is the default. Video detail pages switch to residential IPs on their own because Bilibili blocks datacenter IPs there.

Option 1: no code

  1. Open the actor page and click Try for free. You need a free Apify account.
  2. Keep What to scrape on "Search videos by keyword".
  3. Type your keyword. I used 机械键盘. Copying the Chinese term from Google Translate is fine.
  4. Set Max videos per keyword to 40 and Search sort order to "Most views".
  5. Tick Add full stats to search results if you want coins and shares.
  6. Click Start. When it finishes, open the Output tab and export to CSV, Excel or JSON.

If a video is deleted, private or region locked, it is skipped and listed in the run's ERRORS record. If nothing at all could be scraped, the run fails with a message instead of ending "successful" with an empty dataset.

Option 2: Python with apify-client and pandas

Install the two packages:

pip install apify-client pandas
Enter fullscreen mode Exit fullscreen mode

Then run this. Put your token in an environment variable (APIFY_TOKEN) so it never ends up in your code.

import os
import pandas as pd
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

run_input = {
    "mode": "search",
    "searchQueries": ["机械键盘"],
    "maxResultsPerQuery": 40,
    "sortBy": "click",
    "includeDetails": True,
}

run = client.actor("flarewow/bilibili-video-scraper").call(run_input=run_input)
items = client.dataset(run["defaultDatasetId"]).list_items().items
df = pd.DataFrame(items)

df["published"] = pd.to_datetime(df["publishedAt"])
df["like_rate"] = df["likes"] / df["views"]
df["fav_rate"] = df["favorites"] / df["views"]
df["is_short"] = df["durationSec"] < 60

print(df["views"].describe())
print(df["category"].value_counts().head())
print(df.groupby("is_short")[["like_rate", "fav_rate"]].median())

top_tags = df["tags"].explode().value_counts().head(10)
print(top_tags)

cols = ["rank", "title", "views", "likes", "coins", "favorites",
        "durationSec", "category", "published", "url"]
df[cols].to_csv("bilibili_mechanical_keyboard.csv", index=False, encoding="utf-8-sig")
Enter fullscreen mode Exit fullscreen mode

Two small notes. Use encoding="utf-8-sig" if the CSV will be opened in Excel, or the Chinese titles turn into garbage. And tags is a list per row, which is why explode() is there before counting.

To compare several products, pass more keywords in searchQueries. Each row keeps its searchQuery, so df.groupby("searchQuery") gives you a side by side view.

Scheduling and integrations

For brand or competitor monitoring, switch sortBy to pubdate and create a schedule in Apify Console (Schedules, then pick the actor and a saved input). Daily is enough for most keywords.

The results can go straight to Google Sheets, Slack or a webhook, and the actor works with Make, Zapier and n8n. It is also available through the Apify MCP server, so an AI agent in Claude or ChatGPT can call it as a tool.

Pricing

The actor charges per video saved: $3.50 per 1,000 videos ($0.0035 each), plus a small Apify start fee. Turning on full stats does not change the price. My 40 video run works out to $0.14 in result charges. Failed or empty runs cost nothing for results.

Responsible use

The actor only reads public video pages and public channel names and links. It does not log in, does not collect comment text or data about ordinary users, and does not solve CAPTCHAs. Uploader names are in the data because channels are public, but if you republish results, think about whether you need them. I left them out of the screenshot above. Follow Bilibili's terms and the laws that apply to you, such as GDPR in the EU and PIPL in China.

This tutorial was written with AI assistance and checked by the author. All numbers come from a real run of the scraper.

Top comments (0)