在Python中设置爬虫的URL可以通过以下几种方法:
1. 使用`urlparse`模块解析URL:
```python
from urlparse import urlparse
url = 'https://example.com/path/to/page'
parsed_url = urlparse(url)
2. 使用`requests`库创建请求对象:
```python
import requests
url = 'https://example.com/path/to/page'
response = requests.get(url)
3. 使用`urljoin`函数拼接URL:
```python
from urllib.parse import urljoin
base_url = 'https://example.com'
relative_url = '/path/to/page'
full_url = urljoin(base_url, relative_url)
4. 使用正则表达式提取URL:
```python
import re
html = 'Link text'
url_pattern = re.compile(r'href="(https?://[^"]+)">')
url = url_pattern.search(html).group(1)
5. 使用`selenium`库设置请求头(例如,用于处理JavaScript渲染的页面):
```python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless') 无头模式
driver = webdriver.Chrome(options=options)
url = 'http://www.targetweb.com'
driver.get(url)
请根据您的具体需求选择合适的方法来设置URL。