爬取知乎热榜查看热度

爬取知乎热榜查看热度_第1张图片import requests
from bs4 import BeautifulSoup
headers = {
    'cookie': 'l_n_c=1; q_c1=53034a3f0f514d0f938d2abb4ef4c40a|1543457812000|1543457812000; _xsrf=02cc66747d2794664e1b0a4482b23c4b; n_c=1; d_c0="ANChDPljlw6PTnVZzzoSTjhGb06X2kSFBH0=|1543457814"; _xsrf=oL0XsM0vxNY9rSPgS9XhS2mwZXEVIbkw; _zap=2ba9ae00-8fc4-424b-b0f1-04a4d0ca551a; __utmc=51854390; __utmz=51854390.1543457815.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.000--|3=entry_date=20181128=1; __utma=51854390.1640548042.1543457815.1543457815.1543481275.2; l_cap_id="OTIwODAxMTFjYzUwNDY0ZjkwZDA3YTc5MzQ4MjMzNjM=|1543482775|d517a90435b8e80e591b4aa7d2e453bec5c8f85b"; r_cap_id="YzQ2M2ZjODRlZDJjNDFkY2FmZWI0NzBhOWRmNzUwNTI=|1543482775|049d6cbae1f611a8c7d6794a4513aeee71019e86"; cap_id="NDYzODk2MjE1NjdlNGJjNmFjNTU0ZGM2ZmNhZmRiNTc=|1543482775|90ce66a3790977d948794b2515492d3cb6fc5984"; tgw_l7_route=29b95235203ffc15742abb84032d7e75; capsion_ticket="2|1:0|10:1543558349|14:capsion_ticket|44:MDNjNGRiNTQxNTgzNGI0ZmI4ZjY1MjAxMWM2OWQzNGQ=|12de9ab789bd31818cb5ead3d804d3e6a549b1e1e057761cae503036a4d36a96"; z_c0="2|1:0|10:1543558362|4:z_c0|92:Mi4xa0tBaURRQUFBQUFBMEtFTS1XT1hEaVlBQUFCZ0FsVk4yaUx1WEFDUTBsZGdJeGZZSXlOVXhPZG5wZnBkZTRuVWdR|89dd9d24561fb32c119eccb8e6577224486893a8e357bca10e0c1f721355f8c9"; tst=h; __gads=ID=ba9a29eca6fca2d0:T=1543558671:S=ALNI_MZ4glJtx3a90tkC9gFG1rIowv4lfQ',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
proxies = {
    
}
r = requests.get('https://www.zhihu.com/hot',headers=headers,proxies=proxies)
soup = BeautifulSoup(r.text,'lxml')
# print(soup.prettify())
i=1
for title in soup.find_all(attrs={'class':'HotItem-content'}):
    print('Number '+str(i))
    print(title.a.h2.string)
    if title.a.p :
        print(title.a.p.string)
    print(list(enumerate(title.div))[1])
    i=i+1
    print('-------------------------------------------')

你可能感兴趣的:(爬取知乎热榜查看热度)