Calling requests on byte object (image download) fails with JSON can't serialize
When trying to download images only behind Cloudflare with request, a byte object is returned causing utils.py/write_json to fail with "JSON can't serialize". It looks like that is just part of the normal saving to json action, but for bytes causes an issue.
To reproduce:
@request(use_stealth=True)
def scrape_file(request: Request, url: str):
response = request.get(url)
logging.debug(f"Used Botasaurus Stealth Request to process {url} with {response.status_code}")
file = response.raw
return file
file = scrape_file("https://www.masterofmalt.com/whiskies/praban-na-linne/poit-dhubh-21-year-old-whisky.jpg")
Issue seems to be that write_json always fires and fails on bytes object.
Proposal: Ignore byte objects?
def write_json(data, path, indent=4):
# if data is a bytes object, just ignore it
if isinstance(data, bytes):
return
with open(path, "w", encoding="utf-8") as fp:
json.dump(data, fp, indent=indent)
Discussed in https://github.com/omkarcloud/botasaurus/discussions/169
Originally posted by MaxKarpyza August 3, 2024 I tried bt. save_image and the Driver download_file function. In the first case, the code returned is not 200, in the second case, it does not save anything at all. How else can I save the file? Site for testing https://www.sushifuji.ru/ufa
Please use custom output fn, and save as you wish.
Ah! Thank you, could not find this. Finally found it here: https://github.com/omkarcloud/botasaurus?tab=readme-ov-file#output-and-output_formats
@task(output=None) def scrape_heading_task(data):