webdriver.Chrome() 怎么设置请求次数
时间: 2023-05-12 09:02:38 浏览: 164
您可以使用以下代码来设置请求次数:
```python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-infobars')
options.add_argument('--disable-notifications')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-web-security')
options.add_argument('--disable-logging')
options.add_argument('--log-level=3')
driver = webdriver.Chrome(desired_capabilities=caps, options=options)
driver.execute_cdp_cmd('Network.enable', {})
driver.execute_cdp_cmd('Network.setCacheDisabled', {'cacheDisabled': True})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Referer': 'https://www.google.com/'}})
driver.execute_cdp_cmd('Network.setRequestInterception', {'patterns': [{'urlPattern': '*', 'resourceType': 'Document', 'interceptionStage': 'HeadersReceived'}]})
driver.execute_cdp_cmd('Network.interceptRequest', {'urlPattern': '*', 'resourceType': 'Document', 'interceptionStage': 'HeadersReceived', 'handler': 'Network.continueInterceptedRequest'})
driver.execute_cdp_cmd('Network.setBlockedURLs', {'urls': ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.css', '*.js', '*.svg', '*.woff', '*.woff2', '*.ttf', '*.eot', '*.ico']})
driver.execute_cdp_cmd('Network.setCookie', {'name': 'cookie_name', 'value': 'cookie_value', 'url': 'https://www.google.com/'})
driver.execute_cdp_cmd('Network.setCookies', {'cookies': [{'name': 'cookie_name', 'value': 'cookie_value', 'url': 'https://www.google.com/'}]})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Referer': 'https://www.google.com/'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'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.36'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Accept-Language': 'en-US,en;q=0.9'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Accept-Encoding': 'gzip, deflate, br'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Connection': 'keep-alive'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Upgrade-Insecure-Requests': '1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Cache-Control': 'max-age=0'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'Pragma': 'no-cache'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'TE': 'Trailers'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'DNT': '1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Requested-With': 'XMLHttpRequest'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port': '443'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP-Original': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host-Original': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host-Original': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto-Original': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme-Original': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port-Original': '443'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-For-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Real-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Client-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Remote-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Originating-IP-Client-IP': '127.0.0.1'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Host-Client-IP': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Host-Client-IP': 'www.google.com'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Proto-Client-IP': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Scheme-Client-IP': 'https'}})
driver.execute_cdp_cmd('Network.setExtraHTTPHeaders', {'headers': {'X-Forwarded-Port-Client-IP': '443'}})
url = 'https://www.google.com/'
for i in range(10):
driver.get(url)
```
这段代码可以设置请求次数为10次,您可以根据需要进行修改。
阅读全文