在Python中,你可以使用多种库来获取URL。以下是一些常用的库和方法:
import requests
url = 'https://example.com'
response = requests.get(url)
if response.status_code == 200:
print("URL获取成功:", url)
else:
print("URL获取失败:", url)
from urllib.request import urlopen
url = 'https://example.com'
response = urlopen(url)
if response.status == 200:
print("URL获取成功:", url)
else:
print("URL获取失败:", url)
pip install beautifulsoup4
from bs4 import BeautifulSoup
import requests
url = 'https://example.com'
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a') # 获取所有的<a>标签
for link in links:
print("URL获取成功:", link.get('href'))
else:
print("URL获取失败:", url)
这些方法可以帮助你获取网页上的URL。你可以根据需要选择合适的方法。