To send a user-agent while scraping using Python, you can use the 'requests' library and set the 'headers' parameter in the 'get()' method to include a user-agent string.

Here is an example code snippet:

import requests

url = 'https://www.example.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

response = requests.get(url, headers=headers)

print(response.text)

In this example, we are using a user-agent string for the Google Chrome browser on Windows 10. You can change the user-agent string to any other browser or device you want to emulate.

Python Web Scraping: How to Send a User-Agent

原文地址: https://www.cveoy.top/t/topic/lKT7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录