첫번째 예)
import requests
import keyboard
# Set up a session object to make HTTP requests
session = requests.Session()
# Set up a variable to store the URL to crawl
url = 'https://www.example.com'
# Start crawling the website
while True:
# Check if the user has pressed the 'q' key
if keyboard.is_pressed('q'):
print('Exiting program...')
break
# Make a request to the URL
response = session.get(url)
# Parse the response and extract any relevant information
# ...
# Perform any necessary processing on the extracted information
# ...
# Wait for a short period of time before crawling the website again
time.sleep(10)
------------------------------------------
두번째 예)
import requests
import threading
# Set up a session object to make HTTP requests
session = requests.Session()
# Set up a variable to store the URL to crawl
url = 'https://www.example.com'
# Define a function to check for keyboard input
def check_input():
while True:
if input() == 'q':
print('Exiting program...')
break
# Create a new thread to check for keyboard input
input_thread = threading.Thread(target=check_input)
input_thread.start()
# Start crawling the website
while True:
# Make a request to the URL
response = session.get(url)
# Parse the response and extract any relevant information
# ...
# Perform any necessary processing on the extracted information
# ...
# Check if the input thread has exited
if not input_thread.is_alive():
break
# Wait for a short period of time before crawling the website again
time.sleep(10)
Which one is better?
Both examples achieve the same functionality, but they use different approaches to do so. Here are some factors to consider when deciding which approach to use:
- Simplicity: The first example, which uses a single loop to continuously check for keyboard input, is simpler and easier to understand. It does not require creating and managing a separate thread.
- Responsiveness: The second example, which uses a separate thread to check for keyboard input, allows the program to be more responsive to user input. This is because the web-crawling process and the keyboard input checking process run independently of each other. This can be important if the web-crawling process takes a long time to complete and you want to ensure that the program can be terminated at any time.
- Compatibility: The first example, which uses the input() function to check for keyboard input, may not work well in all environments. For example, if your program is running in a web application or a graphical user interface (GUI), the input() function may not be available or may not work as expected. In contrast, the second example, which uses the keyboard module, is more likely to work in a wider range of environments.
Overall, the choice between the two examples depends on the specific requirements of your program. If simplicity is a priority and you don't need to worry about the responsiveness of the program, then the first example may be sufficient. If you want to ensure that the program can respond to keyboard input quickly and reliably, or if you need to run the program in a wider range of environments, then the second example may be a better choice.
댓글