农机网_多模板页面,无限if抓取(源码)_一蓑烟雨任平生

本文介绍了一种针对农机网的爬虫实现方案,通过不断适应网页结构的变化来抓取新闻标题、发布时间及内容等信息,并将这些数据存入MySQL数据库。示例中详细展示了如何使用Python的requests库获取网页内容,利用BeautifulSoup进行解析,以及如何处理不同页面布局所带来的挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个网站算是比较棘手的了,因为每次标签位置都会变,一会标题在div里,一会再select里,一会又在span里,所以无限判断搞的,到最后我都不知道我在写什么了

废话不多说,直接上代码

今天要倒霉的网站是农机网

# -*- coding: utf-8 -*-
import requests
import pymysql
from bs4 import BeautifulSoup  # 用来解析网页
import uuid
import time

url = "https://www.nongjx.com"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 '
                  'Safari/537.36',
    'Accept-Language': 'zh-CN,zh;q=0.8'
}
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='zhang', charset='utf8')
cur = conn.cursor()
print("连接成功")

for i in range(1, 10):  # 爬取第一页到第3页的数据
    resp = requests.get(f"https://www.nongjx.com/tech_news/t118/list_p{i}.html", headers=headers)
    page_one = BeautifulSoup(resp.content, "html.parser")
    dd = page_one.find('div', class_='mainLeftList').find_all('dt')
    for ss in dd:
        sUrl = url + ss.find('a')['href']
        # 打开二级网页进行爬取
        rp = requests.get(sUrl, headers=headers)
        page_two = BeautifulSoup(rp.content, "html.parser")
        papaer_id = str(uuid.uuid1())
        # 标题
        if page_two.find('section', class_='newsDetail') is None:
            if page_two.find('div', class_='newsDetail') is not None:
                title = page_two.find('div', class_='newsDetail').find('h3').text
                # 时间
                timet = page_two.find('div', class_='newsDetail').find('div').text.strip().split(":")[2]
                print(timet)
                # 内容
                content = page_two.find('div', class_='newsContent').text.strip()
            else:
                if page_two.find('div', class_='nr_box') is None:
                    if page_two.find('div', class_='main_news_details') is None:
                        title = page_two.find('div', class_='newsShow').find('a').text
                        # 时间
                        timet = page_two.find('div', class_='newsShow').find('dt').text.strip()[0:11]
                        print(timet)
                        # 内容
                        content = page_two.find('div', class_='newsContent').text.strip()
                    else:
                        title = page_two.find('div', class_='main_news_details').find('h1').text
                        # 时间
                        timet = page_two.find('div', class_='main_news_details').find('span').text.strip().split(":")[1]
                        print(timet)
                        # 内容
                        content = page_two.find('div', class_='news_detail_content').text.strip()
                else:
                    title = page_two.find('div', class_='nr_box').find('h3').text
                    # 时间
                    timet = page_two.find('div', class_='nr_box').find('p').text.strip().split(":")[2]
                    print(timet)
                    # 内容
                    content = page_two.find('div', class_='down_xx').text.strip()
        else:
            title = page_two.find('section', class_='newsDetail').find('h3').text
            # 时间
            timet = page_two.find('section', class_='newsDetail').find('span').text.strip().split(":")[2]
            print(timet)
            # 内容
            content = page_two.find('div', class_='newsContent').text.strip()
        sql = "insert into knowledge(id,title,timet,content,p_type,url) VALUES (%s,%s,%s,%s,%s,%s)"
        cur.execute(sql, (papaer_id, title, timet, content, "机械农业", sUrl))
    print("SQL正在执行第{}页执行完毕".format(i))
    conn.commit()
    time.sleep(1)  # 防止服务器蹦了,间隔一秒钟
cur.close()
conn.close()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值