Python多线程爬虫:爬取红盾网

本文介绍了一个使用Python实现的多线程爬虫项目,通过XPath解析网页,获取并存储详细信息到CSV文件和MongoDB数据库中。项目涵盖请求模块、时间模块、CSV操作、MongoDB数据库交互及多线程处理。

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

import requests
import time
import csv
import pymongo
from lxml import etree
from multiprocessing import Pool

"""
本项目只用于学习,不用于获取倒卖任何信息
获取网页所有地区的链接详情页信息
Windows10
Pycharm2018
xpath
多线程
数据库
"""


class RedDum(object):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3730.400 QQBrowser/10.5.3805.400'}

    """获取页面详情页链接"""

    def get_pages(self, url):
        link_list = []
        response = requests.get(url=url, headers=self.headers)
        items = etree.HTML(response.text)
        hrefs = items.xpath('//*[@class="search-detail"]')
        for href in hrefs[4:]:
            links = href.xpath('./a/@href')
            for link in links:
                link_list.append(link)
        return link_list


    """解析详情页内容"""

    def paser_detail(self, url):
        response = requests.get(url=url, headers=self.headers)
        time.sleep(1)
        items = etree.HTML(response.text)
        titles = items.xpath('//a[@class="name"]')
        for titl in titles:  # 地区
            title = titl.xpath('./text()')[0]
        lis = items.xpath('//*[@id="list-container"]/ul/li')
        for li in lis:
            daima = li.xpath('./div/p[1]/a/span[1]/text()')[0]  # 代码
            person = li.xpath('./div/p[1]/a/span[2]/text()')[0]  # 法人
            address = li.xpath('./div/p[2]/a/span/text()')[0]  # 地址
            data = [title, daima, person, address]
            collection = {
                '地区': title,
                '代码': daima,
                '法人': person,
                '地址': address
            }
            self.mongodb(collection)
            self.save_details('\n'.join(data).replace(',', '').strip())
            print(title, daima, person, address)

    """多线程"""
    def threads_crawl(self):
        link = self.get_pages('https://www.ubaike.cn/')
        pool = Pool()
        process = pool.map(self.paser_detail, link)
        pool.join()
        pool.close()


    def save_details(self, data):
        with open('hongdun1.csv', 'a', encoding='utf-8')as f:
            write = csv.writer(f)
            write.writerow(data)

    def mongodb(self, collection):
        clinet = pymongo.MongoClient('localhost', 27017)
        db = clinet.hongdun
        table = db.hongdun_table
        table.insert_many(collection)

if __name__ == '__main__':
    k = RedDum()
    k.threads_crawl()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影雀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值