Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [0.86.0] - 2026-12-01

### Added
- **`baidu.search()` returns Baidu's own cards in `SearchResponse.cards`.** Baidu mixes its own cards (Baike, its AI answer, images, goods, notes, a map, latest news ...) with the organic results, and for a popular term they can fill the whole first page, which used to come back as an empty `results`. Each `Card` has `position`, `type`, `title`, `url`, `baidu_url` and `snippet`; every field can be `None`.
- **`Card.position` is Baidu's number for the place on the page.** Organic results and cards share that numbering (1-10 on page 1, 11-20 on page 2). `OrganicResult.position` is unchanged, the rank among the organic results, so the two are not the same sequence.
- **`Card.type`** is Baidu's template name as it is in the page, e.g. `bk_polysemy` (Baike), `new_baikan_index` (the AI answer), `image_grid_san` (images).
- `Card` is exported from `scrapebadger.baidu` and as `scrapebadger.BaiduCard`.
- `results` and every other field are unchanged. `cards` is an empty list on an answer that has none.

## [0.85.0] - 2026-11-30

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Etsy and AliExpress category navigation is separate from search: their search me
| **Gumtree** | 9 endpoints across UK, Australia and South Africa (`market="uk"` default, `"au"`, `"za"`) — search (category, location/postcode radius, price, seller type, urgent/photos, category-specific filters), search filters with live counts, listing detail (full vehicle spec, seller rating/verification, masked phone), seller listings, keyword and location autocomplete, location and category trees, markets. E.g. `client.gumtree.search(market="au", category="18320", location="3003435", distance=10)` or `client.gumtree.search(market="za", category="9077", location="3100003")` | [Gumtree Docs](https://docs.scrapebadger.com/gumtree/overview) |
| **AutoScout24** | 4 endpoints — search, listing detail (URL or UUID), filters, markets | [AutoScout24 Docs](https://docs.scrapebadger.com/autoscout24/overview) |
| **Yelp** | 2 endpoints — business search (location, query, category alias, sort, price, 8 feature refinements, pages 1–11, ads separate) and markets (us, ca). E.g. `client.yelp.search_businesses(location="San Francisco, CA", query="pizza")` | [Yelp Docs](https://docs.scrapebadger.com/yelp/overview) |
| **Baidu** | 4 endpoints — web search (language + date filters), news vertical, image search, autocomplete. Results carry the **real target URL**, not just Baidu's tracking redirect | [Baidu Docs](https://docs.scrapebadger.com/baidu/overview) |
| **Baidu** | 4 endpoints — web search (language + date filters), news vertical, image search, autocomplete. Results carry the **real target URL**, not just Baidu's tracking redirect; web search also lists Baidu's own cards (Baike, AI answer, images ...) in `cards` | [Baidu Docs](https://docs.scrapebadger.com/baidu/overview) |
| **Bing** | 6 endpoints — web search (with ads + related searches), image search, video search, news vertical, autocomplete, markets | [Bing Docs](https://docs.scrapebadger.com/bing/overview) |
| **DuckDuckGo** | 7 endpoints — web search (with abstract box), image/news/video search, autocomplete, instant answers, regions | [DuckDuckGo Docs](https://docs.scrapebadger.com/duckduckgo/overview) |
| **Yahoo** | 6 endpoints across 35 markets — web search (with ads + related searches), image search, video search, news vertical, autocomplete, markets | [Yahoo Docs](https://docs.scrapebadger.com/yahoo/overview) |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "scrapebadger"
version = "0.85.0"
version = "0.86.0"
description = "Official Python SDK for ScrapeBadger - Async web scraping APIs for Twitter and more"
readme = "README.md"
license = { text = "MIT" }
Expand Down
4 changes: 3 additions & 1 deletion src/scrapebadger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async def main():
from scrapebadger.apartments.models import Unit as ApartmentsUnit
from scrapebadger.baidu.client import BaiduClient
from scrapebadger.baidu.models import AutocompleteResponse as BaiduAutocompleteResponse
from scrapebadger.baidu.models import Card as BaiduCard
from scrapebadger.baidu.models import ImageResult as BaiduImageResult
from scrapebadger.baidu.models import ImagesResponse as BaiduImagesResponse
from scrapebadger.baidu.models import NewsResponse as BaiduNewsResponse
Expand Down Expand Up @@ -1356,7 +1357,7 @@ async def main():
ZestimateHistoryPoint as ZillowZestimateHistoryPoint,
)

__version__ = "0.85.0"
__version__ = "0.86.0"

__all__ = [
# TikTok core models
Expand Down Expand Up @@ -1393,6 +1394,7 @@ async def main():
"AuthenticationError",
# Baidu
"BaiduAutocompleteResponse",
"BaiduCard",
"BaiduClient",
"BaiduImageResult",
"BaiduImagesResponse",
Expand Down
2 changes: 1 addition & 1 deletion src/scrapebadger/_internal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
_CONCURRENCY_ERRORS = frozenset({"concurrency_limit_exceeded", "trial_concurrency"})

# User agent for SDK requests
SDK_VERSION = "0.85.0"
SDK_VERSION = "0.86.0"
USER_AGENT = f"scrapebadger-python/{SDK_VERSION}"


Expand Down
3 changes: 3 additions & 0 deletions src/scrapebadger/baidu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from scrapebadger.baidu.client import BaiduClient
from scrapebadger.baidu.models import (
AutocompleteResponse,
Card,
ImageResult,
ImagesResponse,
NewsResponse,
Expand All @@ -47,6 +48,8 @@
"AutocompleteResponse",
# Client
"BaiduClient",
# Baidu's own cards (web search)
"Card",
# Images
"ImageResult",
"ImagesResponse",
Expand Down
8 changes: 7 additions & 1 deletion src/scrapebadger/baidu/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ async def search(
time_to: Unix timestamp — only results published before this.

Returns:
SearchResponse with organic results and Baidu's related searches.
SearchResponse with the organic results, Baidu's own cards
(``cards``: Baike, its AI answer, images, goods ...) and its
related searches. For a popular term the first page can be all
cards, with ``results`` empty.

Raises:
AuthenticationError: If the API key is invalid.
Expand All @@ -103,6 +106,9 @@ async def search(
for r in results.results:
# r.url is the real destination, r.baidu_url the redirect
print(r.title, r.url, r.date_at)
for card in results.cards:
# card.position is Baidu's number for the place on the page
print(card.position, card.type, card.title, card.url)
```
"""
params: dict[str, Any] = {
Expand Down
40 changes: 40 additions & 0 deletions src/scrapebadger/baidu/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,42 @@ class OrganicResult(_BaseModel):
"""Baidu's result template id, e.g. ``se_com_default``, ``www_index``."""


class Card(_BaseModel):
"""One of Baidu's own cards on a web SERP.

Baidu mixes its own cards (Baike, its AI answer, images, goods, notes, a
map, latest news ...) with the organic results, and for a popular term
they can fill the whole first page. Every card is read the same way,
whatever its template.
"""

position: int | None = None
"""Baidu's number for the card's place on the page. Organic results and
cards share this numbering (1-10 on page 1, 11-20 on page 2), so it is
**not** comparable with ``OrganicResult.position``, which is the rank
among the organic results only."""

type: str | None = None
"""Baidu's template name for the card, as it is in the page, e.g.
``bk_polysemy`` (Baike), ``new_baikan_index`` (the AI answer),
``image_grid_san`` (images)."""

title: str | None = None
"""The card's heading."""

url: str | None = None
"""The real target URL when the page gives one, otherwise the same link
as ``baidu_url``."""

baidu_url: str | None = None
"""The heading's own link, usually Baidu's ``baidu.com/link?url=...``
tracking redirect."""

snippet: str | None = None
"""The card's summary text, when it has one. Lists and grids (images,
goods, notes) have none."""


class RelatedSearch(_BaseModel):
"""One of Baidu's related-search suggestions."""

Expand Down Expand Up @@ -101,6 +137,10 @@ class SearchResponse(_BaseModel):
results: list[OrganicResult] = Field(default_factory=list)
"""Organic results on this page."""

cards: list[Card] = Field(default_factory=list)
"""Baidu's own cards on this page, in page order. For a popular term the
page can be all cards and ``results`` empty."""

related_searches: list[RelatedSearch] = Field(default_factory=list)
"""Baidu's related-search suggestions."""

Expand Down
71 changes: 69 additions & 2 deletions tests/test_baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@
"thumbnail": "https://t9.baidu.com/it/u=1053246899,2367519289",
"tpl": "www_index",
}
CARD: dict[str, Any] = {
"position": 4,
"type": "sg_kg_entity_san",
"title": "咖啡机 - 百度百科",
"url": "https://baike.baidu.com/item/%E5%92%96%E5%95%A1%E6%9C%BA/1054960",
"baidu_url": "http://www.baidu.com/link?url=WlGObndkGGpxDKmR",
"snippet": "咖啡机是一种通过电子技术实现自动控制的咖啡冲泡设备",
}
SEARCH_RESPONSE: dict[str, Any] = {
"query": "咖啡机",
"page": 1,
"num": 10,
"total_results": 4_820_000,
"results": [ORGANIC_RESULT],
"cards": [CARD],
"related_searches": [{"query": "咖啡机厂家", "url": "https://www.baidu.com/s?wd=%E5%92%96"}],
"url": "https://www.baidu.com/s?wd=%E5%92%96%E5%95%A1%E6%9C%BA&ie=utf-8",
}
Expand Down Expand Up @@ -190,10 +199,13 @@ async def test_autocomplete_routes(baidu: BaiduClient, mock_base_client: MagicMo
def test_search_response_keeps_every_field() -> None:
parsed = SearchResponse.model_validate(SEARCH_RESPONSE)

assert parsed.model_dump(exclude={"results", "related_searches"}) == {
k: v for k, v in SEARCH_RESPONSE.items() if k not in {"results", "related_searches"}
assert parsed.model_dump(exclude={"results", "cards", "related_searches"}) == {
k: v
for k, v in SEARCH_RESPONSE.items()
if k not in {"results", "cards", "related_searches"}
}
assert parsed.results[0].model_dump() == ORGANIC_RESULT
assert parsed.cards[0].model_dump() == CARD
assert parsed.related_searches[0].query == "咖啡机厂家"


Expand Down Expand Up @@ -235,3 +247,58 @@ def test_nullable_fields_default_to_none() -> None:
assert parsed.related_searches == []
assert parsed.results[0].url is None
assert parsed.results[0].date_at is None


# =============================================================================
# Baidu's own cards (API of 2026-12-01)
# =============================================================================


def test_a_page_of_only_cards_has_cards_and_no_results() -> None:
"""For a popular term Baidu fills page 1 with its own cards: `results` is empty."""
parsed = SearchResponse.model_validate(
{
"query": "咖啡",
"page": 1,
"num": 10,
"total_results": None,
"results": [],
"cards": [
CARD,
{
"position": 2,
"type": "new_baikan_index",
"title": None,
"url": None,
"baidu_url": None,
"snippet": "咖啡是以咖啡豆为原料经烘焙研磨制成的饮品",
},
],
"related_searches": [],
"url": "https://www.baidu.com/s?wd=%E5%92%96%E5%95%A1&ie=utf-8",
}
)

assert parsed.results == []
assert [c.position for c in parsed.cards] == [4, 2]
assert parsed.cards[0].type == "sg_kg_entity_san"
assert parsed.cards[0].url is not None and parsed.cards[0].url.startswith(
"https://baike.baidu.com/"
)
# a card may be a summary only: no heading, no link
assert parsed.cards[1].title is None and parsed.cards[1].url is None
assert parsed.cards[1].snippet is not None


def test_an_answer_without_cards_parses_to_an_empty_list() -> None:
"""Answers from before the API listed cards have no `cards` key."""
answer = {k: v for k, v in SEARCH_RESPONSE.items() if k != "cards"}

assert SearchResponse.model_validate(answer).cards == []


def test_card_is_exported() -> None:
import scrapebadger
from scrapebadger.baidu import Card

assert scrapebadger.BaiduCard is Card
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading