Skip to content

Commit b91ba4a

Browse files
עידן וילנסקיעידן וילנסקי
authored andcommitted
Add parse parameter to search function for JSON parsing support
- Add parse parameter to search() method in both API and client - When parse=True, appends &brd_json=1 to search URLs - Update documentation to describe the new parameter - Add unit test to verify URL construction - Update examples to demonstrate usage - Bump version to 1.0.4
1 parent 448cc3a commit b91ba4a

7 files changed

Lines changed: 41 additions & 12 deletions

File tree

brightdata/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
APIError
3333
)
3434

35-
__version__ = "1.0.3"
35+
__version__ = "1.0.4"
3636
__author__ = "Bright Data"
3737
__email__ = "support@brightdata.com"
3838

brightdata/api/search.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def search(
3535
data_format: str = "markdown",
3636
async_request: bool = False,
3737
max_workers: int = 10,
38-
timeout: int = None
38+
timeout: int = None,
39+
parse: bool = False
3940
) -> Union[Dict[str, Any], str, List[Union[Dict[str, Any], str]]]:
4041
"""
4142
## Search the web using Bright Data SERP API
@@ -54,6 +55,7 @@ def search(
5455
- `async_request` (bool, optional): Enable asynchronous processing (default: `False`)
5556
- `max_workers` (int, optional): Maximum parallel workers for multiple queries (default: `10`)
5657
- `timeout` (int, optional): Request timeout in seconds (default: `30`)
58+
- `parse` (bool, optional): Enable JSON parsing by adding brd_json=1 to URL (default: `False`)
5759
5860
### Returns:
5961
- Single query: `Dict[str, Any]` if `response_format="json"`, `str` if `response_format="raw"`
@@ -116,7 +118,7 @@ def search(
116118
executor.submit(
117119
self._perform_single_search,
118120
single_query, zone, response_format, method, country,
119-
data_format, async_request, base_url, timeout
121+
data_format, async_request, base_url, timeout, parse
120122
): i
121123
for i, single_query in enumerate(query)
122124
}
@@ -133,7 +135,7 @@ def search(
133135
else:
134136
return self._perform_single_search(
135137
query, zone, response_format, method, country,
136-
data_format, async_request, base_url, timeout
138+
data_format, async_request, base_url, timeout, parse
137139
)
138140

139141
def _perform_single_search(
@@ -146,14 +148,18 @@ def _perform_single_search(
146148
data_format: str,
147149
async_request: bool,
148150
base_url: str,
149-
timeout: int
151+
timeout: int,
152+
parse: bool
150153
) -> Union[Dict[str, Any], str]:
151154
"""
152155
Perform a single search operation
153156
"""
154157
encoded_query = quote_plus(query)
155158
url = f"{base_url}{encoded_query}"
156159

160+
if parse:
161+
url += "&brd_json=1"
162+
157163
endpoint = "https://api.brightdata.com/request"
158164

159165
payload = {

brightdata/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def search(
207207
data_format: str = "markdown",
208208
async_request: bool = False,
209209
max_workers: int = None,
210-
timeout: int = None
210+
timeout: int = None,
211+
parse: bool = False
211212
) -> Union[Dict[str, Any], str, List[Union[Dict[str, Any], str]]]:
212213
"""
213214
## Search the web using Bright Data SERP API
@@ -226,6 +227,7 @@ def search(
226227
- `async_request` (bool, optional): Enable asynchronous processing (default: `False`)
227228
- `max_workers` (int, optional): Maximum parallel workers for multiple queries (default: `10`)
228229
- `timeout` (int, optional): Request timeout in seconds (default: `30`)
230+
- `parse` (bool, optional): Enable JSON parsing by adding brd_json=1 to URL (default: `False`)
229231
230232
### Returns:
231233
- Single query: `Dict[str, Any]` if `response_format="json"`, `str` if `response_format="raw"`
@@ -264,7 +266,7 @@ def search(
264266

265267
return self.search_api.search(
266268
query, search_engine, zone, response_format, method, country,
267-
data_format, async_request, max_workers, timeout
269+
data_format, async_request, max_workers, timeout, parse
268270
)
269271

270272
def download_content(self, content: Union[Dict, str], filename: str = None, format: str = "json") -> str:

examples/scrape_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
results = client.scrape(url=URL, max_workers=5)
1414

15-
client.download_content(results)
15+
client.download_content(results)

examples/search_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
from brightdata import bdclient
77

8-
client = bdclient(api_token="your-api-key",auto_create_zones=False, serp_zone="your-custom-serp-zone") # zone and API token can also be defined in .env file
8+
client = bdclient(api_token="your-api-token", auto_create_zones=False, serp_zone="your-custom-serp-zone") # zone and API token can also be defined in .env file
99

1010
query = ["iphone 16", "coffee maker", "portable projector", "sony headphones",
1111
"laptop stand", "power bank", "running shoes", "android tablet",
1212
"hiking backpack", "dash cam"]
1313

14-
results = client.search(query, search_engine="bing", max_workers=10)
14+
results = client.search(query, max_workers=10)
1515

16-
print(results)
16+
client.download_content(results)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "brightdata-sdk"
7-
version = "1.0.3"
7+
version = "1.0.4"
88
description = "Python SDK for Bright Data Web Scraping and SERP APIs"
99
authors = [
1010
{name = "Bright Data", email = "support@brightdata.com"}

tests/test_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ def test_search_unsupported_engine(self, client):
9494
"""Test unsupported search engine validation"""
9595
with pytest.raises(ValidationError, match="Invalid search engine"):
9696
client.search("test query", search_engine="invalid_engine")
97+
98+
def test_search_with_parse_parameter(self, client, monkeypatch):
99+
"""Test search with parse parameter adds brd_json=1 to URL"""
100+
# Mock the session.post method to capture the request
101+
captured_request = {}
102+
103+
def mock_post(*args, **kwargs):
104+
captured_request.update(kwargs)
105+
from unittest.mock import Mock
106+
response = Mock()
107+
response.status_code = 200
108+
response.text = "mocked html response"
109+
return response
110+
111+
monkeypatch.setattr(client.search_api.session, 'post', mock_post)
112+
113+
result = client.search("test query", parse=True)
114+
115+
# Verify the request was made with correct URL containing &brd_json=1
116+
request_data = captured_request.get('json', {})
117+
assert "&brd_json=1" in request_data["url"]
97118

98119

99120
if __name__ == "__main__":

0 commit comments

Comments
 (0)