Skip to main content
 首页 » 编程设计

python之使用Python和BeautifulSoup访问网页标签的title属性

2025年05月04日14jackei

我是 Python 的新手,我想从一个特定的 url 中检索所有标题,但我无法这样做。代码编译没有任何错误,但我仍然没有得到输出。

import requests 
import sys 
from bs4 import BeautifulSoup 
 
def test_function(num): 
    url = "https://www.zomato.com/chennai/restaurants?buffet=1&page=" +        
    str(num) 
    source_code = requests.get(url)  
    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text) 
    for link in soup.findAll('title'): 
        print(link) 
test_function(1) 

请您参考如下方法:

要获取页面标题,您可以简单地使用:

soup.title.string 

但是,您似乎并不真正想要页面标题,而是想要包含标题的任何标签的属性。如果你想获得每个标签的 title 属性(如果它存在的话)那么你可以这样做:

for tag in soup.findAll(): 
    try: 
        print(tag['title']) 
    except KeyError: 
        pass 

这将打印页面中标签的所有标题。我们查看所有标签,尝试打印它的标题值,如果没有,我们将得到一个 KeyError,然后我们对错误不做任何处理!

还有一个问题是不通过请求传递用户代理。如果您不这样做,该站点将给出 500 错误。我在下面添加了代码来执行此操作。

用你的代码会是

import requests 
import sys 
from bs4 import BeautifulSoup 
 
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"} 
 
def test_function(num): 
    url = "https://www.zomato.com/chennai/restaurants?buffet=1&page=" +        
        str(num) 
    source_code = requests.get(url, headers=HEADERS)  
    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text) 
    for tag in soup.findAll(): 
        try: 
            print(tag['title']) 
        except KeyError: 
            pass 
 
test_function(1)