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)