以下是 Python 代码,可以批量查询网站标题:

import requests
from bs4 import BeautifulSoup

# 读取网址列表
with open('urls.txt', 'r') as f:
    urls = f.readlines()

# 遍历网址列表
for url in urls:
    url = url.strip()  # 去除换行符等空白字符
    try:
        # 发送HTTP请求,获取网页内容
        response = requests.get(url)
        response.encoding = response.apparent_encoding
        html = response.text

        # 使用BeautifulSoup解析网页内容,获取标题
        soup = BeautifulSoup(html, 'html.parser')
        title = soup.title.string

        # 输出网址和标题
        print(url, title)
    except:
        # 若发生错误,则输出错误信息
        print(url, 'Error')

将需要查询的网址列表保存在一个名为“urls.txt”的文本文件中,每行一个网址。运行上述代码,即可批量查询每个网址的标题,并输出到控制台。

Python 批量查询网站标题代码

原文地址: https://www.cveoy.top/t/topic/jsD8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录