python用xpath方法爬取伯乐在线

1.xpath的用法

 

2.提取信息

(1)在命令行中可以测试

response.xpath("//p[@class='entry-meta-hide-on-mobile']/text()").extract()[0].strip().replace("·","")

(2)在pycharm中运行

import scrapy
import re

class JobboleSpider(scrapy.Spider):
name = 'jobbole'
allowed_domains = ['blog.jobbole.com']
start_urls = ['http://blog.jobbole.com/113728/']

def parse(self, response):
title1=response.xpath("//*[@id='post-113728']/div[1]/h1/text()").extract()[0]
title=response.xpath("//div[@class='entry-header']/h1/text()").extract()[0]
creat_time=response.xpath("//p[@class='entry-meta-hide-on-mobile']/text()").extract()[0].strip().replace("·","")
praise_num=response.xpath("//span[contains(@class,'vote-post-up')]/h10/text()").extract()[0]

fav_nums= response.xpath("//span[contains(@class,' bookmark-btn')]/text()").extract()[0]
match_re=re.match(".*(\d+).*",fav_nums)
if match_re:
fav_nums=match_re.group(1)

comment_nums=response.xpath("//a[@href='#article-comment']/span").extract()[0]
match_re=re.match(".*(\d+).*",comment_nums)
if match_re:
comment_nums=match_re.group(1)
cotent=response.xpath("//div[@class='entry']").extract()[0]

tag_list = response.xpath("//p[@class='entry-meta-hide-on-mobile']/a/text()").extract()
tag_list = [element for element in tag_list if not element.strip().endswith("评论")]
tags = ",".join(tag_list)
pass

 

转载于:https://www.cnblogs.com/lin1318/p/8583061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值