网站标题批量查询源码:Python 代码示例
由于不确定您指的是哪种网站标题批量查询源码,下面提供两种不同的解释和示例代码:
- 批量查询多个网站的标题
示例代码:
import requests
from bs4 import BeautifulSoup
urls = ['https://www.example1.com', 'https://www.example2.com', 'https://www.example3.com']
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string
print(f'Title of {url}: {title}')
解释:该代码使用 requests 库向多个网站发送 GET 请求,然后使用 BeautifulSoup 库解析 HTML 页面并提取标题,最后输出每个网站的标题。
- 批量查询一个网站的多个页面的标题
示例代码:
import requests
from bs4 import BeautifulSoup
base_url = 'https://www.example.com/page'
for i in range(1, 11):
url = f'{base_url}{i}.html'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string
print(f'Title of {url}: {title}')
解释:该代码使用 requests 库向同一个网站的多个页面发送 GET 请求,然后使用 BeautifulSoup 库解析 HTML 页面并提取标题,最后输出每个页面的标题。在这个示例中,我们假设该网站的页面 URL 是类似于 https://www.example.com/page1.html、https://www.example.com/page2.html 等的格式,因此我们使用了一个循环来生成这些 URL。
原文地址: https://www.cveoy.top/t/topic/jsrp 著作权归作者所有。请勿转载和采集!