importrequestsfrombs4importBeautifulSoupurlhttp:price。naif。org。twQueryQuerynow。aspxheaders{useragent:Mozilla5。0(WindowsNT10。0;Win64;x64)appleWebKit537。36(KHTML,likeGecko)Chrome91。0。4472。124Safari537。36}resprequests。get(url,headersheaders)设定编码为utf8避免中文乱码问题resp。encodingutf8根据HTTPheader的编码解码後的内容资料(ex。UTF8),若该网站没设定可能会有中文乱码问题。所以通常会使用resp。encoding设定rawhtmlresp。text将HTML转成BeautifulSoup物件soupBeautifulSoup(rawhtml,html。parser)print(soup。select(htmlbodyform1div。wrapdiv。contantdiv:nthchild(7)divContentPlaceHoldercontentGridViewdatatbodytr:nthchild(4)td:nthchild(2)))