CSS Selectors
You can use CSS Selectors for data extraction. In the table below, you will find a list of examples of how to use it. You only need to add&css_extractor={"links":"a @href"} to the request to use this feature.
Here are some examples
| extraction rules | sample html | value | json output |
|---|---|---|---|
| {“divs”:“div”} | <div>text0</div> | text | {“divs”: “text0”} |
| {“divs”:“div”} | <div>text1</div><div>text2</div> | text | {“divs”: [“text1”, “text2”]} |
| {“links”:“a @href”} | <a href=“#register”>Register</a> | href attribute | {“links”: “#register”} |
| {“hidden”:“input[type=hidden] @value”} | <input type=“hidden” name=“_token” value=“f23g23g.b9u1bg91g.zv97” /> | value attribute | {“hidden”: “f23g23g.b9u1bg91g.zv97”} |
| {“class”:“button.submit @data-v”} | <button class=“submit” data-v=“register-user”>click</button> | data-v attribute with submit class | {“class”: “register-user”} |
| {“emails”:“a[href^=‘mailto:’] @href”} | <a href=“mailto:test1@domain.com”>email 1</a><a href=“mailto:test2@domain.com”>email 2</a> | href attribute for links starting with mailto: | {“emails”: [“test1@domain.com”, “test2@domain.com”]} |
| {“id”:“#my-id”} | <div id=“my-id”>Content here</div> | Content from element with id | {“id”: “Content here”} |
| {“links”:“a[id=‘register-link’] @href”} | <a id=“register-link” href=“#signup”>Sign up</a> | href attribute of element with specific id | {“links”: “#signup”} |
| {“xpath”:“//h1”} | <h1>Welcome</h1> | Extract text using XPath | {“xpath”: “Welcome”} |
| {“xpath”:“//img @src”} | <img src=“image.png” alt=“image description” /> | Extract src attribute using XPath | {“xpath”: “image.png”} |
Auto Parsing
ZenRows® API will return the HTML of the URL by default. Enabling Autoparse uses our extraction algorithms to parse data in JSON format automatically. Add&autoparse=true to the request for this feature.
Output Filters
Theoutputs parameter lets you specify which data types to extract from the scraped HTML. This allows you to efficiently retrieve only the data types you’re interested in, reducing processing time and focusing on the most relevant information.
The parameter accepts a comma-separated list of filter names and returns the results in a structured JSON format.
Here’s an example of how to use the outputs parameter:
Emails
Extracts email addresses using CSS selectors and regular expressions. This includes standard email formats likeexample@example.com and obfuscated versions like example[at]example.com.
Example: outputs=emails
output
Phone Numbers
Extracts phone numbers using CSS selectors and regular expressions, focusing on links withtel: protocol.
Example: outputs=phone_numbers
output
Headings
Extracts heading text from HTML elementsh1 through h6.
Example: outputs=headings
output
Images
Extracts image sources fromimg tags. Only the src attribute is returned.
Example: outputs=images
output
Audios
Extracts audio sources fromsource elements inside audio tags. Only the src attribute is returned.
Example: outputs=audios
output
Videos
Extracts video sources fromsource elements inside video tags. Only the src attribute is returned.
Example: outputs=videos
output
Links
Extracts URLs froma tags. Only the href attribute is returned.
Example: outputs=links
output
Menus
Extracts menu items fromli elements inside menu tags.
Example: outputs=menus
output
Hashtags
Extracts hashtags using regular expressions, matching typical hashtag formats like#example.
Example: outputs=hashtags
output
Metadata
Extracts meta-information frommeta tags inside the head section. Returns name and content attributes in the format name: content.
Example: outputs=metadata
output
Tables
Extracts data fromtable elements and returns the table data in JSON format, including dimensions, headings, and content.
Example: outputs=tables
output
Favicon
Extracts the favicon URL from thelink element in the head section of the HTML.
Example: outputs=favicon
output
Markdown Response
By addingresponse_type=markdown to the request parameters, the ZenRows API will return the content in a Markdown format, making it easier to read and work with, especially if you are more comfortable with Markdown than HTML.
It can be beneficial if you prefer working with Markdown for its simplicity and readability.
Add response_type=markdown to the request:
Plain Text Response
Theplaintext feature is an output option that returns the scraped content as plain text instead of HTML or Markdown.
This feature can be helpful when you want a clean, unformatted version of the content without any HTML tags or Markdown formatting. It simplifies the content extraction process and makes processing or analyzing the text easier.
Add response_type=plaintext to the request:
plaintext_response feature, ZenRows Universal Scraper API will convert this HTML content into plain text like this:
PDF Response
In today’s data-driven world, the ability to generate and save web scraping results in various formats can significantly enhance data utilization and sharing. To use the PDF response feature, you must include thejs_render=true parameter alongside with the response_type with the value pdf in your request. This instructs the API to generate a PDF file from the scraped content.
The resulting PDF file will contain the same information as the web page you scraped.
.pdf you can save it using the following example in Python:
scraper.py
Page Screenshot
Capture an above-the-fold screenshot of the target page by addingscreenshot=true to the request. By default, the image will be in PNG format.
Additional Options
screenshot_fullpage=truetakes a full-page screenshot.screenshot_selector=<CSS Selector>takes a screenshot of the element given in the CSS Selector.
screenshot_selector and screenshot_fullpage are mutually exclusive. Additionally, JavaScript rendering (js_render=true) is required.
These screenshot features can be combined with other options like wait, wait_for, or js_instructions to ensure that the page or elements are fully loaded before capturing the image. When using json_response, the result will include a JSON object with the screenshot data encoded in base64, allowing for easy integration into your workflows.
Image Format and Quality
In addition to the basic screenshot functionality, ZenRows offers customization options to optimize the output. These features are particularly useful for reducing file size, especially when taking full-page screenshots where the image might exceed 10MB, causing errors.screenshot_format: Choose betweenpngandjpegformats, with PNG being the default. PNG is great for high-quality images and transparency, while JPEG offers efficient compression.screenshot_quality: Applicable when using JPEG, this parameter allows you to set the quality from1to100. Useful for balancing image clarity and file size, especially in scenarios where storage or bandwidth is limited.
Download Files and Pictures
ZenRows® lets you download images, PDFs, and other files directly from web pages. This feature is handy when extracting non-text content, like product images, manuals, or downloadable reports, as part of your web scraping workflow. Example:1
Direct File Response
If the URL you request returns the file directly, such as an image or PDF link, ZenRows will fetch the file so you can save it in its original format. This is the most reliable method.
2
Triggered Downloads Using JS Instructions
If a file download is started by a user action, such as clicking a button or link, you can use ZenRows’ JS Instructions to simulate these actions. If the download begins automatically, without prompting for a directory or further user input, ZenRows can capture and return the file.
Downloads are only possible when the file is delivered directly in the HTTP response. If the website asks the user to choose a download location or requires more interaction, ZenRows cannot capture the file. In these cases, we recommend using our Scraping Browser, which gives you more control over the browser session and supports more complex interactions.
File Size Limits
ZenRows enforces a maximum file size per request to ensure stable performance. If you try downloading a file larger than your plan allows, you will receive a413 Content Too Large error.
You can find more details on the plan limits on our Pricing Documentation
Frequently Asked Questions (FAQ)
Can I use multiple response_type formats together?
Can I use multiple response_type formats together?
No, certain
response_type formats like Markdown, Plain Text, and PDF cannot be used together.Why am I getting the original content instead of the specified response_type (Markdown, Plain Text, or PDF)?
Why am I getting the original content instead of the specified response_type (Markdown, Plain Text, or PDF)?
For us to process the response as a
response_type (Markdown, Plain Text, or PDF), we need to be able to parse the response as HTML. If we can’t parse the response as HTML, we’ll return the original response.When can this happen? When the response type is not text/html or when the response is not rendered.How do I control the image quality and format for screenshots?
How do I control the image quality and format for screenshots?
ZenRows supports customizing screenshots. You can choose between PNG (default) or JPEG formats using
screenshot_format. For JPEGs, you can control the quality using screenshot_quality, with a value between 1 and 100, to balance image clarity and file size.How can I ensure that dynamic pages are fully loaded before scraping?
How can I ensure that dynamic pages are fully loaded before scraping?
To ensure dynamic pages are fully loaded before scraping, you can use JavaScript rendering (
js_render=true) and pair it with parameters like wait or wait_for. This ensures that ZenRows waits until the necessary elements are present on the page before scraping.