99作文网、微博、双色球、汽车之家

本文介绍了如何使用爬虫技术分别从99作文网、微博平台、双色球彩票网站以及汽车之家获取数据,详细阐述了各个网站的数据结构和爬取策略,为读者提供了全面的爬虫实战经验。
摘要由CSDN通过智能技术生成

99作文网

import requests
from lxml import etree
import urllib.parse
import os


class ZuoWenSpider():
    def __init__(self):
        self.url = 'https://www.99zuowen.com/xiaoxuezuowen/'
        self.headers = {
   
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
        }

        self.dir = '99zuowen/'
        if not os.path.exists(self.dir):
            os.mkdir(self.dir)

    def send_request(self, full_url):
        response = requests.get(full_url, headers=self.headers)

        if response.status_code == 200:
            return response

    def parse_content(self, response):  # 解析内容
        html = etree.HTML(response.text)
        cat = html.xpath('//dl[@class="type_list2"]')[0]
        a_list = cat.xpath('./dd/a')

        for a in a_list:
            title = a.xpath('./text()')[0]
            href = a.xpath('./@href')[0]
            full_url = urllib.parse.urljoin(self.url, href)
            response = self.send_request(full_url)
            self.parse_list(response, title)

    def parse_list(self, response, title):  # 解析列表
        html = etree.HTML(response.text)
        href_list = html.xpath('//li[@class="lis"]//span[@class="right"]/a/@href')

        for href in href_list:
            response = self.send_request(href)
            self.parse_detail(response, title)

    def parse_detail(self, response, title):  # 解析详情

        html = etree.HTML(response.text)
        name = html.xpath('//h1/text()')[0]
        content = "".join(html.xpath('//div[@class="content"]//p//text()'))
        self.save_content(title, name, content
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值