Baidu
Classes
BaiDu
Bases: BaseSearchEngine[BaiDuResponse]
API client for the BaiDu image search engine.
Used for performing reverse image searches using BaiDu service.
Attributes:
Name | Type | Description |
---|---|---|
base_url |
str
|
The base URL for BaiDu searches. |
Source code in PicImageSearch/engines/baidu.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
Functions
__init__(**request_kwargs)
Initializes a BaiDu API client with specified configurations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**request_kwargs
|
Any
|
Additional arguments for network requests. |
{}
|
Source code in PicImageSearch/engines/baidu.py
22 23 24 25 26 27 28 29 |
|
_extract_card_data(data)
staticmethod
Extracts 'window.cardData' from the BaiDu search response page.
This method parses the JavaScript content in the page to find and extract the 'window.cardData' object, which contains the search results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
PyQuery
|
A PyQuery object containing the parsed HTML page. |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: A list of card data dictionaries, where each dictionary contains information about a search result. Returns an empty list if no card data is found. |
Note
The method searches for specific script tags containing 'window.cardData' and extracts the JSON data between the first '[' and last ']' characters.
Source code in PicImageSearch/engines/baidu.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
search(url=None, file=None, **kwargs)
async
Performs a reverse image search on BaiDu.
This method supports two ways of searching
- Search by image URL
- Search by uploading a local image file
The search process involves multiple steps
- Upload the image or submit the URL to BaiDu
- Follow the returned URL to get the search results page
- Extract and parse the card data from the page
- If similar images are found, fetch the detailed results
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
Optional[str]
|
URL of the image to search. |
None
|
file
|
Union[str, bytes, Path, None]
|
Local image file, can be a path string, bytes data, or Path object. |
None
|
**kwargs
|
Any
|
Additional arguments passed to the parent class. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaiDuResponse |
BaiDuResponse
|
An object containing the search results and metadata. Returns empty results if no matches are found or if the 'noresult' card is present. |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
Note
- Only one of
url
orfile
should be provided. - The search process involves multiple HTTP requests to BaiDu's API.
- The response format varies depending on whether matches are found.
Source code in PicImageSearch/engines/baidu.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|