一小时教会你单线程爬取微博热搜

【Python爬虫】单线程爬取微博热搜

最近有很多小伙伴们都天天在微博上吃到各种不少的瓜吧,一打开微博热搜榜就是当下的热点头条。那么我们怎么用程序来爬取微博热搜的内容呢?
今天我就来教会大家怎么用爬虫爬取微博热搜上的内容,可以随时随地在自己电脑上run一下就可以获取到当下微博热搜。

首先,什么是爬虫呢?

网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。 ——《百度百科》

我们写爬虫用的是Python语言(一般写爬虫程序都是选择Python)
关于Python如何安装我就不在这里赘述了,大家可自行到网上去百度下载,网上也有各种教程教你安装Python环境,安装起来也很简单。

写爬虫程序一般要用到一些第三方库,比如requests,bs4,xpath…
安装方法:打开cmd;输入:

pip install requests #例如安装requests

写爬虫第一步,导入需要的第三方库(也可以什么时候需要什么时候加)

import requests
from bs4 import BeautifulSoup
from urllib import parse
import time
  • 然后,我们需要微博热搜的网址,即url。URL=https://s.weibo.com/top/summary?cate=realtimehot
  • 所谓爬虫就是我们用程序模拟人类行为去请求访问服务器,然后服务器会给我们回应,返回网页内容。

定义第一个函数:请求网页内容。这里我们先给出一部分代码:

headers = {
     
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
}    #模拟浏览器行为

def get_url(url):
    response = requests.get(url,headers=headers)
    if response.status_code == 200:
        response.encoding = 'utf-8'
        return response.text
    else:
        return ""

当我们得到请求的网页内容后,我们需要将网页内容转换为我们所要的格式来输出。(网页内容即网页源代码,浏览器经过JavaScript渲染后就是我们在浏览器上看到的丰富的页面内容,爬虫里没有JavaScript的支持,所以返回的是网页源代码)

下面我们来定义第二个函数:获取网页内容
这里我们需要用到BeautifulSoup库,将源代码变成我们要的数据格式。
这部分代码如下:

def get_html(html):
    soup = BeautifulSoup(html,'lxml')
    trs = soup.select('table tbody tr')
    for tr in trs:
        title = tr.select_one('td a').text
        link = tr.select_one('td a')['href']
        link = parse.urljoin('https://s.weibo.com/',link)
        print(title,link)

最后,程序需要一个主函数,即main(),主函数用来调用函数,另外URL等内容也可以放在主函数中。

if __name__ == '__main__':
    start = time.time()
    url = 'https://s.weibo.com/top/summary?cate=realtimehot'
    url2 = 'https://s.weibo.com/top/summary?cate=socialevent'
    html = get_url(url)
    get_html(html)
    html2 = get_url(url2)
    get_html(html2)
    print(time.time()-start) #用来检测程序运行时间

完整代码如下

import requests
from bs4 import BeautifulSoup
from urllib import parse
import time

headers = {
     
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36'
}    #模拟浏览器行为

def get_url(url):
    response = requests.get(url,headers=headers)
    if response.status_code == 200:
        response.encoding = 'utf-8'
        return response.text
    else:
        return ""

def get_html(html):
    soup = BeautifulSoup(html,'lxml')
    trs = soup.select('table tbody tr')
    for tr in trs:
        title = tr.select_one('td a').text
        link = tr.select_one('td a')['href']
        link = parse.urljoin('https://s.weibo.com/',link)
        print(title,link)

if __name__ == '__main__':
    start = time.time()
    url = 'https://s.weibo.com/top/summary?cate=realtimehot'
    url2 = 'https://s.weibo.com/top/summary?cate=socialevent'
    html = get_url(url)
    get_html(html)
    html2 = get_url(url2)
    get_html(html2)
    print(time.time()-start)

展示效果如下
一小时教会你单线程爬取微博热搜_第1张图片
一小时教会你单线程爬取微博热搜_第2张图片
有疑问的可以在评论区评论,欢迎大家积极讨论,也可以私信博主哦!

你可能感兴趣的:(爬虫,python,爬虫)