[파이썬] requests-html 타임아웃 설정

In web scraping or web crawling tasks, it is often necessary to set a timeout for making HTTP requests. This helps in handling unresponsive or slow websites and prevents the program from getting stuck indefinitely. In this blog post, we will explore how to set timeouts using the requests-html library in Python.

What is requests-html?

requests-html is a Python library that provides a convenient way to make HTTP requests and parse HTML content. It is built on top of the popular requests library and includes several advanced features for web scraping, such as JavaScript rendering and CSS selector support.

Setting Timeout in requests-html

To set a timeout value for HTTP requests using requests-html, you can pass the timeout parameter to the get() method. Here’s an example:

from requests_html import HTMLSession

session = HTMLSession()
response = session.get('http://example.com', timeout=10)

In the above code snippet, the timeout parameter is set to 10 seconds, which means that if the website doesn’t respond within 10 seconds, the request will be aborted and a TimeoutError will be raised.

You can adjust the timeout value according to your specific needs. If you don’t specify a timeout value, the request will wait indefinitely for a response.

Handling TimeoutError

When a TimeoutError occurs due to a timeout, you can handle it gracefully by catching the exception and taking appropriate actions. For example:

from requests_html import HTMLSession

session = HTMLSession()
try:
    response = session.get('http://example.com', timeout=10)
except TimeoutError:
    print("Request timed out. Please try again later or adjust the timeout value.")

In the above code, we catch the TimeoutError exception and display an informative message to the user.

Conclusion

In this blog post, we learned how to set timeouts in requests-html Python library. By setting appropriate timeout values, we can handle unresponsive websites and prevent our program from hanging indefinitely. Remember to adjust the timeout value according to your specific needs.