import requests#獲得請求 2 from bs4 import BeautifulSoup#解析的第三方庫 3 import pandas as pd 5 url = 'http://top.zhihu.com/buzz.php?p=top10&tdsourcetag=s_pctim_aiomsg&qq-pf-to=pcqq.c2c?' 6 kv = {'user-agent': 'Mozilla/5.0'}#偽裝爬蟲 7 respond = requests.get(url,timeout = 30,headers=kv) 8 r.raise_for_status() 9 r.encoding = r.apparent_encoding 10 r.text#獲取源代碼 11 html=r.text 12 soup=BeautifulSoup(html,'html.parser') 13 #解析網頁,提取內容 14 q = []#創建空列表 15 w = [] 16 for x in soup.find_all(class_="list-title")[:10]: 17 q.append(x.get_text().strip()) 18 for y in soup.find_all(class_="icon-rise")[:10]: 19 w.append(y.get_text().strip()) 20 text =['q,w'] 21 print(text) 22 c=pd.DataFrame(text,index=['標題','熱度']) 23 print(new file)