import requests from bs4 import BeautifulSoup url = "http://price.naif.org.tw/Query/Query_now.aspx" headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) appleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"} resp = requests.get(url, headers=headers) # 设定编码为 utf-8 避免中文乱码问题 resp.encoding = "utf-8" # 根据 HTTP header 的编码解码後的内容资料(ex. UTF-8),若该网站没设定可能会有中文乱码问题。所以通常会使用 resp.encoding 设定 raw_html = resp.text # 将 HTML 转成 BeautifulSoup 物件 soup = BeautifulSoup(raw_html, "html.parser") print(soup.select("html > body > #form1 > div.wrap > div.contant > div:nth-child(7) > div > #ContentPlaceHolder_content_GridView_data > tbody > tr:nth-child(4) > td:nth-child(2)"))