随笔 - 58  文章 - 1  评论 - 5  阅读 - 10万

Python爬虫实践——爬取网站文章

初学Python,对爬虫也是一知半解,恰好有个实验需要一些数据,所以本次爬取的对象来自中国农业信息网中下属的一个科技板块种植技术的文章(http://www.agri.cn/kj/syjs/zzjs/

首先,分析网站结构:各文章标题以列表的形式展示,点击标题获得则是文章的正文,如图所示:

分析网页源码,不难看出图一所示的URL规律,其首页为 http://www.agri.cn/kj/syjs/zzjs/index.htm ,其后续页面分别为 http://www.agri.cn/kj/syjs/zzjs/index_1.htm 、http://www.agri.cn/kj/syjs/zzjs/index_2.htm …… 等等以此类推,因此可以使用循环对URL来赋值得到想要的页数。

下一步,获取新闻的标题和URL,通过解析网页源码,发现所有标题包含在如下结构中:

得到结构后,可以据此使用Beautifulsoup得到所需的a标签链接和标题,并将其存入dictionary中,使用key保存标题,而URL作为其value值

最后,将得到的dictionary遍历,取出每个链接并解析网页代码,得到需要的文章信息,最后一并存进数据库,代码如下:

复制代码
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import requests
import sys
import pymysql
import re

#--------set page amount----------

def set_download_urls():
    downloadUrls = []
    baseUrl = 'http://www.agri.cn/kj/syjs/zzjs/'
    downloadUrls.append('http://www.agri.cn/kj/syjs/zzjs/index.htm')
    for i in range(1,10):
        url = baseUrl + 'index_' + str(i) + '.htm'
        downloadUrls.append(url)
    return downloadUrls


#--------get download page urls

def get_download_tables():
    downloadUrls = set_download_urls()
    tables = []
    for url in downloadUrls:
        req = requests.get(url)
        req.encoding = 'utf-8'
        html = req.text
        table_bf = BeautifulSoup(html)
        tables.append(table_bf.find('table',width=500,align='center'))

    return tables

#---------get article links------------
def get_download_url():
    downloadTables = get_download_tables()
    articles = []
    for each in downloadTables:
        articles.append(each.find_all('a',class_='link03'))
    return articles

def read_article_info():
    articles = get_download_url()
    baseUrl = 'http://www.agri.cn/kj/syjs/zzjs'
    dict = {}

    for each in articles:
        for item in each:
            dict[item.string] = baseUrl + item.get('href')[1:]
    return dict


#---------method of save to MySQL-----------

def save_mysql(title,date,source,content,tech_code,info_code):
    db = pymysql.connect('localhost','root','123456','persona')

    cursor = db.cursor()

    sql = 'INSERT INTO information_stock (title,date,source,content,tech_code,info_code) VALUES ("%s","%s","%s","%s",%s,%s)' % (title,date,source,content,tech_code,info_code)

    try:
        cursor.execute(sql)
        db.commit()
        print("write success")
    except Exception as e:
        db.rollback()
        print("write fail")
        print(e)
    
    db.close()


#---------get content info and save ---------------

def get_content(title,url):
    print(title + '---->' + url)

    req = requests.get(url)
    req.encoding = 'utf-8'
    html = req.text
    table_bf = BeautifulSoup(html)
    article = table_bf.find('table',width=640)

    #----article content-----
    #content = article.find(class_='TRS_Editor').get_text()
    #content = article.find('div',attrs={'id':re.compile("TRS_")}).select("p")
    content = article.select("p")
    info = article.find(class_='hui_12-12').get_text()
    date = info[3:19]
    source = info.split("")[3]
    text = ""

    for item in content:
        text += item.get_text()
        text += "\n"

    #print(text)
    save_mysql(title,date,source,text,0,0)
        

#--------save all article -----------

def save_data():
    dict = read_article_info()
    for key,value in dict.items():
        get_content(key,value)
    

save_data()
复制代码

 爬取结果入库:

 

posted on   lie隼  阅读(17054)  评论(1编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示