import requestsfrom bs4 import BeautifulSoup# 设置请求头headers = User-Agent Mozilla50 Windows NT 100; Win64; x64 AppleWebKit53736 KHTML like Gecko Chrome5803029110 Safari5373# 请求页面数据r = requestsgethttpsww
import requests from bs4 import BeautifulSoup
设置请求头
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'}
请求页面数据
response = requests.get('https://www.example.com', headers=headers)
使用BeautifulSoup解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
找到页面中的所有链接
links = soup.find_all('a')
输出链接的文本和URL
for link in links: print(link.text, link['href'])
原文地址: https://www.cveoy.top/t/topic/1q3 著作权归作者所有。请勿转载和采集!