如何爬取网页代码
使用Python可以使用requests和BeautifulSoup库来爬取网页:
import requests
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到页面中的所有超链接
links = soup.find_all('a')
for link in links:
print(link.get('href'))
# 找到页面中所有的图片链接
images = soup.find_all('img')
for image in images:
print(image.get('src'))
以上代码先使用requests库发送HTTP请求获取网页内容,然后使用BeautifulSoup库解析HTML页面,可以使用find_all()方法来查找页面中所有符合要求的元素,例如超链接和图片链接。
原文地址: https://www.cveoy.top/t/topic/bhRT 著作权归作者所有。请勿转载和采集!