在Python3中,要绕过反爬虫机制,你可以采取以下策略:
import requests
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"
}
url = "https://example.com"
response = requests.get(url, headers=headers)
import requests
proxies = {
"http": "http://proxy.example.com:8080",
"https": "https://proxy.example.com:8080"
}
url = "https://example.com"
response = requests.get(url, proxies=proxies)
import time
import requests
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"
}
url = "https://example.com"
response = requests.get(url, headers=headers)
time.sleep(5) # 等待5秒
import requests
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",
"Cookies": "your_cookies_here"
}
url = "https://example.com"
response = requests.get(url, headers=headers)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
请注意,绕过反爬虫机制可能会违反网站的使用条款。在进行爬虫开发时,请确保遵守相关法律法规和网站的使用政策。