From fd6942f9beef3a7f400ebf2bb5eb4b19a61c3444 Mon Sep 17 00:00:00 2001 From: kasparasizi1 <132673909+kasparasizi1@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:28:34 +0200 Subject: [PATCH] feat(baidu): search returns Baidu's own cards in SearchResponse.cards (0.86.0, SCR-760) 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 came back as an empty `results`. The API now lists them in `cards` (scrape-badger/scrapebadger#3658). - `Card`: position, type, title, url, baidu_url, snippet, every field optional. `position` is Baidu's number for the place on the page, shared with the organic results; `OrganicResult.position` is unchanged (rank among organic results). - `SearchResponse.cards` defaults to an empty list, so answers without the key parse as before. - Exported from `scrapebadger.baidu` and as `scrapebadger.BaiduCard`. - No method signature changes. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 9 ++++ README.md | 2 +- pyproject.toml | 2 +- src/scrapebadger/__init__.py | 4 +- src/scrapebadger/_internal/client.py | 2 +- src/scrapebadger/baidu/__init__.py | 3 ++ src/scrapebadger/baidu/client.py | 8 +++- src/scrapebadger/baidu/models.py | 40 ++++++++++++++++ tests/test_baidu.py | 71 +++++++++++++++++++++++++++- uv.lock | 2 +- 10 files changed, 135 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84f24e2..83ba50a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 9e4534c..a55e09b 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/pyproject.toml b/pyproject.toml index 231178d..f0f0e78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" } diff --git a/src/scrapebadger/__init__.py b/src/scrapebadger/__init__.py index 367af22..f8c0021 100644 --- a/src/scrapebadger/__init__.py +++ b/src/scrapebadger/__init__.py @@ -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 @@ -1356,7 +1357,7 @@ async def main(): ZestimateHistoryPoint as ZillowZestimateHistoryPoint, ) -__version__ = "0.85.0" +__version__ = "0.86.0" __all__ = [ # TikTok core models @@ -1393,6 +1394,7 @@ async def main(): "AuthenticationError", # Baidu "BaiduAutocompleteResponse", + "BaiduCard", "BaiduClient", "BaiduImageResult", "BaiduImagesResponse", diff --git a/src/scrapebadger/_internal/client.py b/src/scrapebadger/_internal/client.py index a1f35b3..8f2b9ac 100644 --- a/src/scrapebadger/_internal/client.py +++ b/src/scrapebadger/_internal/client.py @@ -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}" diff --git a/src/scrapebadger/baidu/__init__.py b/src/scrapebadger/baidu/__init__.py index 5b696d7..c37a74d 100644 --- a/src/scrapebadger/baidu/__init__.py +++ b/src/scrapebadger/baidu/__init__.py @@ -32,6 +32,7 @@ from scrapebadger.baidu.client import BaiduClient from scrapebadger.baidu.models import ( AutocompleteResponse, + Card, ImageResult, ImagesResponse, NewsResponse, @@ -47,6 +48,8 @@ "AutocompleteResponse", # Client "BaiduClient", + # Baidu's own cards (web search) + "Card", # Images "ImageResult", "ImagesResponse", diff --git a/src/scrapebadger/baidu/client.py b/src/scrapebadger/baidu/client.py index e2df32a..99343dc 100644 --- a/src/scrapebadger/baidu/client.py +++ b/src/scrapebadger/baidu/client.py @@ -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. @@ -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] = { diff --git a/src/scrapebadger/baidu/models.py b/src/scrapebadger/baidu/models.py index d803fad..faaeb8e 100644 --- a/src/scrapebadger/baidu/models.py +++ b/src/scrapebadger/baidu/models.py @@ -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.""" @@ -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.""" diff --git a/tests/test_baidu.py b/tests/test_baidu.py index 91524e3..90d118f 100644 --- a/tests/test_baidu.py +++ b/tests/test_baidu.py @@ -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", } @@ -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 == "咖啡机厂家" @@ -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 diff --git a/uv.lock b/uv.lock index 946da05..b6dc92c 100644 --- a/uv.lock +++ b/uv.lock @@ -930,7 +930,7 @@ wheels = [ [[package]] name = "scrapebadger" -version = "0.85.0" +version = "0.86.0" source = { editable = "." } dependencies = [ { name = "httpx" },