spider———循环爬取花火所有期刊

本文介绍了一种使用Python的BeautifulSoup和lxml库进行网页爬取的方法,详细解释了如何处理多页内容的抓取,通过实例展示了如何定位并提取HTML中的特定信息。

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

熟悉soup和xpath方法,寻找Html标签

import os
import shutil
import time
import urllib.request
from bs4 import BeautifulSoup
from lxml import etree



def handle_request(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36',
    }
    request = urllib.request.Request(url=url,headers=headers)
    return request


def get_content(text_src):
    request = handle_request(text_src)
    text_content = urllib.request.urlopen(request).read().decode('utf8')
    soup = BeautifulSoup(text_content,'lxml')
    other = soup.select('.pagelink > ul > li > b')
    if other == []:
        content_list = soup.select('.zw > p')
        content_all = ''
        for content in content_list:
            content1 = content.text
            # print(content)
            content_all += content1 + '\n'
        return content_all
    else:
        text_src = text_src
        others = int(other[0].text)
        content_all = ''
        for i in range(1,others+1):
            text_src_new = text_src.split('.')
            text_src_content = text_src_new[0] +'.'+ text_src_new[1] +'.'+ text_src_new[2] + '_%d.' % i + text_src_new[-1]
            if i == 1:
                text_src_content = text_src
            # print(text_src_content)
            request = handle_request(text_src_content)
            response = urllib.request.urlopen(request).read().decode('utf8')
            soup = BeautifulSoup(response, 'lxml')
            content_list = soup.select('.zw > p')
            content_all1 = ''
            for content in content_list:
                content1 = content.text
                # print(content)
                content_all1 += content1 + '\n' + '\n'
            content_all += content_all1

        # print(content_all)
        return content_all




def parse_catalog_text(catalog_text,title):
    tree = etree.HTML(catalog_text)
    catalog_text_src = tree.xpath('//div[@class="boxcon"]/ul//h3/a/@href')
    catalog_text_name = tree.xpath('//div[@class="boxcon"]/ul//h3/a/text()')
    for i in range(0,len(catalog_text_src)):
        text_src = 'https://m.feiyanqing.com' + catalog_text_src[i]
        text_name = catalog_text_name[i]
        print("正在下载--%s--...." % text_name)
        fp = open('%s.txt' % text_name, 'w', encoding='utf8')
        content = get_content(text_src)
        # print(content)
        fp.write(text_name + content)
        print("结束下载--%s--.." % text_name)
        time.sleep(2)
        fp.close()
        shutil.move('%s.txt' % text_name,title)



def parse_catalog_content(catalog_content,dirname):
    soup = BeautifulSoup(catalog_content,'lxml')
    catalog = soup.select('.boxcon > ul > li > p > a')
    num = int(input("请输入下载总目录个数--"))
    for log in range(0,num):
        title = catalog[log].string
        # print("正在下载--%s--...." % title)
        if not os.path.exists(title):
            os.mkdir(title)
        catalog_src = 'https://m.feiyanqing.com' + catalog[log].attrs['href']
        request = handle_request(catalog_src)
        catalog_text = urllib.request.urlopen(request).read().decode('utf8')
        parse_catalog_text(catalog_text,title)
        shutil.move(title,dirname)
        # print("结束下载--%s--.." % title)
        time.sleep(2)



def main():
    dirname = '花火'
    if not os.path.exists(dirname):
        os.mkdir(dirname)
    url = 'https://m.feiyanqing.com/huahuo/'
    request = handle_request(url)
    catalog_content = urllib.request.urlopen(request).read().decode('utf8')
    parse_catalog_content(catalog_content,dirname)
    # print(catalog_content)


if __name__ == '__main__':
    main()


### 使用 Spider-Flow 实现多级页面爬取 Spider-Flow 是一种用于构建复杂网络爬虫的工作流工具,能够处理多种类型的网页抓取需求。对于多级页面的爬取,通常涉及多个请求阶段以及数据传递过程。 #### 初始化项目并安装依赖库 为了启动一个新的 Spider-Flow 项目,首先需要创建 Python 虚拟环境,并安装必要的包: ```bash pip install spiderflow requests beautifulsoup4 lxml ``` #### 定义工作流程节点 在 Spider-Flow 中定义不同级别的页面访问逻辑作为独立的任务节点。每个节点代表一次 HTTP 请求及其对应的解析操作。 #### 编写主入口脚本 `main.py` 下面是一个简单的例子来展示如何设置一个多层嵌套结构来进行多级页面的数据收集[^1]: ```python from spiderflow import Flow, Task class ListPageTask(Task): async def run(self): response = await self.fetch('https://example.com/list') soup = BeautifulSoup(response.text, 'lxml') items = [] for link in soup.select('.item-link'): item_url = urljoin('https://example.com', link['href']) items.append({'url': item_url}) return {'items': items} class DetailPageTask(Task): async def run(self, input_data): detail_urls = input_data.get('items', []) details = [] for url in detail_urls[:5]: # 只获取前五个链接的内容 resp = await self.fetch(url['url']) doc = BeautifulSoup(resp.text, 'lxml') title = doc.find('h1').get_text(strip=True) content = ''.join([p.get_text() for p in doc.select('#content p')]) details.append({ "title": title, "content": content }) return {"details": details} if __name__ == '__main__': flow = Flow() list_task = ListPageTask(name="list_page") detail_task = DetailPageTask(name="detail_page") flow.add(list_task).add(detail_task) result = flow.execute() print(result.output()) ``` 此代码片段展示了两个主要部分:`ListPageTask`负责从列表页提取各个条目的 URL;而 `DetailPageTask`则接收这些 URL 并进一步抓取详情页的信息。通过这种方式可以有效地完成对具有层次关系网站内容的自动化采集
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值