爬取知乎热榜

允我心安 提交于 2020-03-19 19:44:53

1.打开网页https://tophub.today/n/mproPpoq6O

 

 

2.按Ctrl+U 打开网页源代码

3.寻找爬取内容

4.

import requests
import pandas as pd
from bs4 import BeautifulSoup
import pandas as pd
lst=[]#建立一个空列表
url='https://tophub.today/n/mproPpoq6O'#所抓取网页的网址
def get(url):
    try:
        headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}#伪装爬虫
        url = requests.get(url,timeout = 30,headers=headers)#发送请求
        url.raise_for_status()
        url.encoding='utf-8'
        return url.text 
    except:
        return "产生异常"
#创建一个放置数据的文件夹
def create(lst,html,num):
        soup=BeautifulSoup(html,'html.parser')
        a=soup.find_all('span',class_='t')
        b=soup.find_all('span',class_='e')
        print('{:^10}\t{:^30}\t{:^10}'.format('排名', '标题', '热度'))
        for i in range(num):
            print('{:^10}\t{:^30}\t{:^10}'.format(i+1, a[i+50].string, b[i+50].string))#打印出所爬取的内容
            lst.append([i+1,a[i+50].string,b[i+50].string])#将爬取的数据放入列表中
html=get(url)
create(lst,html,10)
df=pd.DataFrame(lst,columns=['排名','标题','热度'])
ZHHot='D:\Python\知乎热搜'
df.to_excel(ZHHot)

5.爬取数据

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!