温馨提示×

python爬虫怎么获取url

小亿
81
2024-12-07 07:13:31
栏目: 编程语言

在Python中,你可以使用多种库来获取URL。以下是一些常用的库和方法:

  1. 使用requests库:
import requests

url = 'https://example.com'
response = requests.get(url)

if response.status_code == 200:
    print("URL获取成功:", url)
else:
    print("URL获取失败:", url)
  1. 使用urllib库:
from urllib.request import urlopen

url = 'https://example.com'
response = urlopen(url)

if response.status == 200:
    print("URL获取成功:", url)
else:
    print("URL获取失败:", url)
  1. 使用BeautifulSoup库(需要先安装):
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。你可以根据需要选择合适的方法。

0