2023-06-03 初试python爬取文章

注意:本实验是在windows系统下操作。

首先配置python环境以及安装一些必要的库:

安装python请前往python官网下载,仙人指路👉https://www.python.org/ftp/python/3.11.3/python-3.11.3-amd64.exe

下载完并安装成功后,在终端输入

py --version
输出:Python 3.11.3

检查pip是否安装成功,ps:一般现在安装python都会带有pip;

pip --version

或者

pip3 --version
 输出pip 22.3.1

如果都不输出版本号,可以查看一下系统变量有没有配置了pip的路径,没有配置就配置一下:

找到python安装路径,打开环境变量-系统变量,选择path,选择新建,把python script文件夹路径复制进去即可。

实在是没有安装可以到这里安装下载pip:https://pip.pypa.io/en/stable/installation/#

上面两都有版本号了即表示第一步成功,接着安装4个库:

pip install requests
pip install beautifulsoup4
pip install xlwt
pip install pymysql
各自库的作用是:
requests:获取网页数据
BeautifulSoup:网页解析
xlwt:进行excel操作
pymysql:连接mysql数据库

这样第二步就算完成了,第三步:新建hello.py,输入代码:

# -*- coding = utf-8 -*-
# @Time : 2022/4/27 8:21
# @Author :王敬博
# @File : test.py
# @Software: PyCharm
from bs4 import BeautifulSoup  #网页解析
import re    #正则表表达式文字匹配
import urllib.request,urllib.error  #指定url,获取网页数据
import xlwt  #进行excel操作
import sqlite3  #进行SQLite数据库操作
import pymysql.cursors  #连接mysql数据库

def main():
    baseurl = "https://www.cnblogs.com/iuniko/default.html?page="
    datalist = getData(baseurl)         #调研分析数据函数
    #1 爬取网页
    savepath = ".\\iuniko的博客信息.xls"     #excel保存的位置名称
    saveData(datalist,savepath)          #调用保存函数



def askURL(url):
    head = {   #伪装请求头,模拟浏览器访问
       "User-Agent":" Mozilla / 5.0(Linux;Android6.0;Nexus5 Build / MRA58N) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 99.0.4844.51Mobile Safari / 537.36"
    }
    request = urllib.request.Request(url,headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode('utf-8')
        #print(html)
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    return html  #返回爬到所有的html数据

#正则表达式控制获取详细内容
findTitle = re.compile(r'<span>(.*?)</span>',re.S)
findlink = re.compile(r'<a class="postTitle2 vertical-middle" href="(.*?)">')
findzhaiyao = re.compile(r'<div class="c_b_p_desc"(.*?)<a',re.S)
finddate = re.compile(r'<div class="postDesc">posted @(.*?)哎呦你可棒棒了',re.S)
findread = re.compile(r'<span class="post-view-count" data-post-id=(.*?)</span>')
findcomment = re.compile(r'<span class="post-comment-count" data-post-id=(.*?)</span>')
finddigg = re.compile(r'<span class="post-digg-count" data-post-id=(.*?)</span>')

def getData(baseurl):
    datalist = []  # 2 解析数据
    allTitle = []  #存储标题
    allLink = []   #存储链接
    allZhaiyao = [] #存储摘要
    alldate = []    #存储日期
    allRead = []    #存储阅读数
    allComment = [] #存储评论数
    allDigg = []    #存储推荐数
    for i in range(0,44): # 注意:45代表爬取45页,我的公开的随笔数量是440,所以我要写成45,也就是会爬取450条数据
        url = baseurl + str(i+1)    #对目标链接地址page=后面的数字进行循环
        html = askURL(url)          #调用爬取的函数
        soup = BeautifulSoup(html, "html.parser")

        for item in soup.find_all('div',class_="day"):  #简单过滤信息
            #print(item)

            item = str(item)

            title = re.findall(findTitle,item)     #匹配数据
            allTitle.extend(title)      #添加数据到列表

            link = re.findall(findlink,item)
            allLink.extend(link)

            zhaiyao = re.findall(findzhaiyao,item)
            allZhaiyao.extend(zhaiyao)

            date = re.findall(finddate,item)
            alldate.extend(date)

            readNum = re.findall(findread,item)
            allRead.extend(readNum)

            commentNum = re.findall(findcomment,item)
            allComment.extend(commentNum)

            diggNum = re.findall(finddigg,item)
            allDigg.extend(diggNum)
    print('allTitle ===')
    print(len(allTitle))
    print('allLink ===')
    print(len(allLink))
    print('allZhaiyao ===')
    print(len(allZhaiyao))
    print('alldate ===')
    print(len(alldate))
    #print(allRead)
    #print(allComment)
    #print(allDigg)

    for j in range(0,440):      #循环10页就是100条数据,45页就是450条数据 #这个循环是过滤掉所有不需要的信息
        data = []
        title = allTitle[j].strip()     #去除字符串里的空格
        data.append(title)

        link = allLink[j]
        data.append(link)

        zhaiyao = allZhaiyao[j].strip()
        zhaiyao = zhaiyao.split('">')[1]
        data.append(zhaiyao.replace("\n",""))

        date = alldate[j].strip()
        data.append(date)

        readNum = allRead[j].split('>')[1]      #通过分割字符串来去除无用信息
        data.append(readNum)

        commentNum = allComment[j].split('>')[1]
        data.append(commentNum)

        diggNum = allDigg[j].split('>')[1]
        data.append(diggNum)

        datalist.append(data)

    print(datalist)
    return datalist     #返回列表

def saveData(datalist,savepath):
    print("save...")
    book = xlwt.Workbook(encoding="utf-8",style_compression=0)
    sheet = book.add_sheet('博客园随笔列表',cell_overwrite_ok=True)    #创建sheet1
    col = ("标题","原文链接","摘要","时间","阅读","评论","推荐")        #加标题
    for i in range(0,7):
        sheet.write(0,i,col[i])
    for i in range(0,440):          #添加数据到excel*(我公开的随笔一共有440条)
        print("第%d条"%(i+1))        #打印条数
        data = datalist[i]
        for j in range(0,7):
            sheet.write(i+1,j,data[j])      #添加每个子列表中的7个数据
    book.save(savepath)            #保存excel

if __name__ == "__main__":
    main()
    print("爬取完毕!")

hello.py的代码来自作者:https://www.cnblogs.com/wjingbo/

py代码原文可前往👉:https://www.cnblogs.com/wjingbo/p/16199464.html

该py代码的作用是爬取指定的网站路径,然后把该路径里的相关内容取出放在excel表格里。

好了,保存并运行hello.py:

py hello.py

等待几秒即可看见生成了一个excel表格,里面是爬取https://www.cnblogs.com/wjingbo/他的文章内容。

如果你运行这个py文件出错了,报错内容还是 list index out of range(列表索引超出范围)?

解决方案::你可以尝试去打印一下某个变量的值是否为空,这个报错是因为某个变量的值为空了所以就报了这个错。

 

posted @ 2023-06-03 11:48  叶乘风  阅读(13)  评论(0编辑  收藏  举报