1. 程式人生 > 其它 >Python 網路爬蟲權威指南 第一章 練習

Python 網路爬蟲權威指南 第一章 練習

技術標籤:爬蟲python

獲取網頁的標題:

from urllib.request import urlopen
from urllib.error import URLError
from bs4 import BeautifulSoup


def get_title(url):
    try:
        html = urlopen(url)
    except URLError as e:
        return None
    try:
        bs = BeautifulSoup(html.read(), 'html.parser')
        title =
bs.body.h1 except AttributeError as e: return None return title my_title = get_title('https://www.alibabacloud.com/zh/' 'knowledge/what-is-cloud-computing?spm=a3c0i.243649.2033761600.2.a974d9130g0iYV') if my_title is None: print('Title could not be found.') else: print
(my_title)