scrapy爬虫系列之四--爬取列表和详情

news/2024/7/19 11:23:01 标签: python, 爬虫

功能点:如何爬取列表页,并根据列表页获取详情页信息?

爬取网站:东莞阳光政务网

完整代码:https://files.cnblogs.com/files/bookwed/yangguang.zip

主要代码:

yg.py

import scrapy
from yangguang.items import YangguangItem


class YgSpider(scrapy.Spider):
    name = 'yg'
    allowed_domains = ['sun0769.com']
    start_urls = ['http://wz.sun0769.com/index.php/question/report']

    def parse(self, response):
        tr_list = response.xpath("//div[@class='greyframe']/table[2]//tr")
        for tr in tr_list:
            item = YangguangItem()
            item["title"] = tr.xpath("./td[2]/a[2]/text()").extract_first()
            item["href"] = tr.xpath("./td[2]/a[2]/@href").extract_first()
            item["status"] = tr.xpath("./td[3]/span/text()").extract_first()
            item["publish_time"] = tr.xpath("./td[last()]/text()").extract_first()
            if type(item["href"]) == str:
                # 请求详情页
                yield scrapy.Request(
                    item["href"],
                    callback=self.parse_detail,
                    meta={"item": item}
                )

        # 翻页
        next_url = response.xpath("//a[text()='>']/@href").extract_first()
        if next_url is not None:
            yield scrapy.Request(next_url, callback=self.parse)

    # 解析详情页
    def parse_detail(self, response):
        item = response.meta["item"]
        # 获取详情页的内容、图片
        item["content"] = response.xpath("//div[@class='wzy1']/table[2]//tr[1]/td[@class='txt16_3']/text()").extract()
        item["content_image"] = response.xpath("//div[@class='wzy1']/table[2]//tr[1]/td[@class='txt16_3']//img/@src").extract()
        item["content_image"] = ["http://wz.sun0769.com"+i for i in item["content_image"]]
        yield item  # 对返回的数据进行处理

pipelines.py

class YangguangPipeline(object):
    def __init__(self):
        self.f = open('yangguang.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        item["content"] = self.process_content(item["content"])
        self.f.write(json.dumps(dict(item), ensure_ascii=False) + ',\n')
        return item

    def process_content(self, content):
        # 对内容项里的\xa0 和 空白字符替换为空
        content = [re.sub(r"\xa0|\s", "", i) for i in content]
        # 对替换过的空字符串去除
        content = [i for i in content if len(i) > 0]
        return content

 

转载于:https://www.cnblogs.com/bookwed/p/10617789.html


http://www.niftyadmin.cn/n/910647.html

相关文章

HTML_初识HTML_基础知识

我们浏览网站的时候,看到的丰富的影响、文字、图片,而这些内容都是通过HTML语言来表现出来的。对于网页设计和制作人员,尤其是开发动态网站的标称人员来说,制作网页的时候,如果不涉及HTML语言,几乎是不可能…

为什么要学习算法和数据结构

网络上有很多文章都描述了算法的重要性,包括大公司面试,有大量的算法题。如果算法弱,那么极大可能与大公司错过。下面谈一谈为什么算法和数据结构重要,并且算法知识需要沉淀。 作为Java web开发人员,如果刚开始没有进入…

HTML_html\head\body标签详解

<html>和</html>限定了文档的开始和结束点。 <html>标签详解&#xff1a; dir属性指定了浏览器该用什么方向来显示包含在元素中的文本&#xff0c;具体显示的效果则看浏览器对HTML4的支持程度。ltr:从左向右显示、rtl:从右向左显示&#xff1b;lang属性指定…

[LeetCode] Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 思路1&…

HTML_水平线详解

水平分隔线可以从视觉上将文档分割成各个不同的部分。这样可以给浏览者一个整洁而明确的视觉指示&#xff0c;告诉他们文档一个部分已经结束&#xff0c;而另一个部分即将开始。水平分隔线有效的将文本分割成小块&#xff0c;并界定了文档的页首和页尾&#xff0c;另外还突出了…

learning ddr seft-refresh mode summary

转载于:https://www.cnblogs.com/lianghong881018/p/10619215.html

对于有志于成为架构师的开发者,支付宝架构团队有何建议?

技术不是一蹴而就的事情&#xff0c;而是长时间积累的成果。此外&#xff0c;扎实的基本功是做好所有事情的开始&#xff01;抽象的能力也是作为一名好的程序员必须具备的&#xff0c;我们在考虑问题的时候可能会遇到错综复杂的场景&#xff0c;从这些迷雾中找到一条明路是我们…

HTML_列表详解

列表定义&#xff1a; 除了专门的文本标签来修饰文本外&#xff0c;HTML还提供了打量的工具&#xff0c;帮助将文本内容组织成格式化表格。而列表的优点就存在于它的简单。 列表分类&#xff1a; 列表分为&#xff1a;无序列表、有序列表、目录列表、定义列表、菜单列表。 列…