关于scrapy新闻爬虫,对新闻网页内容进行编辑的问题

  • 一般内容

一般某一个网站的新闻页面,标题,作者,日期这几个一般肯定是固定格式的,意思就是同一个网站的新闻“标题”,“作者”都会放在固定html标签里,且标签的class或者id都是有理可依的,而新闻内容一般也会放到一个固定id或者class的div里,已投资界的新闻网页为例,如这篇文章:徒子文化完成数千万人民币A轮融资,腾讯出资

获取标题,作者,内容的代码如下:

# 拼接字符串数组到一个字符串
def get_text(self, texts):
    text = ""
    if len(texts) > 0:
        for tmp in texts:
            text = text + tmp
    return text.strip()

def parse(self, response):
	# 获取标题
	article_titles = response.xpath('//h1[@id="newstitle"]/text()').extract()
	if (article_titles.count > 0):
		print "article_title:" + article_titles[0]
		item["article_title"] = article_titles[0]

	# 获取日期
	article_times = response.xpath('//div[@class="info"]/div[@class="box-l"]/span[@class="date"]/text()').extract()
	item["article_time"] = self.get_text(article_times)
	# print "article_time:" + item["article_time"]

	# 获取作者和文章来源
	article_authors = response.xpath('//div[@class="info"]/div[@class="box-l"]/text()').extract()
	author = self.get_text(article_authors)
	author_list = author.split()
	item["article_src"] = author_list[0]
	if len(author_list) > 1:
		item["article_author"] = author_list[1]
	else:
		item["article_author"] = ""
	# print "article_src:" + item["article_src"]
	# print "article_author:" + item["article_author"]

	# 获取文章概要
	article_summarys = response.xpath('//div[@class="news-show-r"]/div[@class="subject"]/text()').extract()
	item["article_summary"] = self.get_text(article_summarys)
	# print "article_summary:" + item["article_summary"]

  • 图片替换

对于新闻内容中间的图片,有时候我们图片是从原网站的链接,而是我们自己的服务器,这时候需要遍历新闻内容中所有的图片并将图片的链接地址替换掉,其中img是scrapy中的Selector对象,而img.root是lxml中的Element对象,有兴趣的可以去查看下LXml的相关资料, 代码如下:

# 查出html中所有的img
article_content_imgs = response.xpath('//div[@id="news-content"]//img')
# 下载、替换微信文章中的图片
for img in article_content_imgs:
    # 取出img中src
    img_src = self.get_text(img.xpath('./@src').extract())
    # 取出后缀如jpeg或png
    img_type = img_src.split(".")[-1]
    # 如果是bmp、jpg、png、jpeg之外的格式默认为jpg
    if img_type.lower().strip() != "bmp" and img_type.lower().strip() != "jpg" and img_type.lower().strip() != "png" and img_type.lower().strip() != "jpeg":
        img_type = "jpg"

    # 取时间戳
    timestamp = self.get_time_stamp()
    # 这个是网页html中图片的相对路径
    abs_path = u'/Uploads/Spider/News/Content/' + timestamp + '.' + img_type
    # 这个是文件系统中图片的完整路径
    save_path = u'D:/UserUploadFiles勿删' + abs_path

    # 如果原html中图片路径不是http开头,则要将原图片路径地址补全,以用来下载
    if False == img_src.startswith("http"):
        img_src = response.urljoin(img_src)

    # 下载原图片到文件系统
    urllib.urlretrieve(img_src.encode("utf8"), save_path)
    # 替换img中的src,替换成我们自己的相对路径地址
    img.root.attrib['src'] = abs_path

要修改LXml中img的src属性后,再取出内容,这时候div(id=news-content)中所有的img就被我们替换了

article_contents = response.xpath('//div[@id="news-content"]').extract()

  • 删除新闻内容里面不必要的内容

有些其他网站新闻内容,在顶部或者底部都有自己网站的logo或者版权声明,对于这些节点,我们要删除掉,通过lxml Element的drop_tree()方法可以删除节点

# 删除是logo-head123.png的img节点
head_imgs = response.xpath('//div[@class="content"]//img[contains(@src, "logo-head123.png")]')
for img in head_imgs:
    img.root.drop_tree()


# 删除包含文本 “授权请联络”的p节点
bottom_ps = response.xpath(u'//div[@class="content"]//p[contains(./text(), "授权请联络")]')
for p in bottom_ps:
    p.root.drop_tree()


# 删除包含"商务合作"字样的节点的整个父节点
bottom_ps = response.xpath(u'//div[@class="content"]//span[contains(./text(), "商务合作")]')
for p in bottom_ps:
    p.root.getparent().drop_tree()



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Scrapy是一个Python编写的、开源的网络爬虫框架,它可用于从网站提取结构化的数据。它使用了Twisted异步网络框架,可以同时处理多个请求,支持基于XPath、CSS选择器等方式的数据解析,并提供了强大的扩展机制。 下面介绍Scrapy的基本使用: 1. 安装Scrapy Scrapy可以通过pip安装,使用以下命令: ``` pip install scrapy ``` 2. 创建Scrapy项目 在终端中,使用以下命令创建Scrapy项目: ``` scrapy startproject <project_name> ``` 该命令将创建一个名为`<project_name>`的文件夹,其中包含了Scrapy项目的基本结构。 3. 创建Spider 在Scrapy中,Spider是用于爬取网站的主要组件。在项目文件夹中,使用以下命令创建Spider: ``` scrapy genspider <spider_name> <start_url> ``` `<spider_name>`为Spider的名称,`<start_url>`为Spider开始爬取的URL。 创建Spider后,可以在`<project_name>/spiders`文件夹中找到该Spider的Python文件,其中包含了Spider的具体实现。 4. 编写Spider 在Spider的Python文件中,需要定义Spider的名称、允许爬取的域名、起始URL、解析响应的方法等。以下是一个简单的Spider示例: ```python import scrapy class MySpider(scrapy.Spider): name = 'myspider' allowed_domains = ['example.com'] start_urls = ['http://www.example.com/'] def parse(self, response): # 解析响应 pass ``` 在上述示例中,Spider的名称为`myspider`,允许爬取的域名为`example.com`,起始URL为`http://www.example.com/`。解析响应的方法为`parse()`,在该方法中可以使用XPath或CSS选择器等方式解析响应中的数据。 5. 运Spider 在终端中,使用以下命令运Spider: ``` scrapy crawl <spider_name> ``` 该命令将启动Spider,开始爬取网站并解析响应中的数据。 以上是Scrapy的基本使用方法,更多详细内容可以参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值