使用xpath 定位 p标签,定位到了,但取不到内容。。。,爬虫:番组计划

这是我爬取的目标网站

start_url:   http://bangumi.tv/person/1/works/voice

在抓取角色页的日文名字和名字的href属性时,都成功了,详细的日文名字的定位xpath语法如下:

role_item["role_japanese_name"] = role.xpath('./div[@class="ll innerLeftItem"]//h3/a/text()')[0] if len(role.xpath('./div[@class="ll innerLeftItem"]//h3/a/text()')) > 0 else ''

对于同级p标签,中文名字使用相同定位语法理论上也可以拿到,代码如下:

role_item["role_chinese_name"] = role.xpath('./div[@class="ll innerLeftItem"]//h3/p/text()')[0] if len(role.xpath('./div[@class="ll innerLeftItem"]//h3/p/text()')) > 0 else ''

但是拿不到中文名字啊,修改了半天,最后改成这样,代码如下(具体原因,自己也没有找到。。。):

role_item["role_chinese_name"] = role.xpath('.//p/text()')[0] if len(role.xpath('.//p/text()')) > 0 else ''

完整代码如下:

# 9、处理角色
                # 获取角色链接    例子:  'http://bangumi.tv/person/5076/works/voice'
                role_lists = list()
                role_link = html.xpath('//*[@id="headerSubject"]//a[contains(text(),"角色")]/@href')
                # print(role_link)
                # print(url)
                if len(role_link) > 0:
                    '''处理角色详情页情况'''
                    role_link = 'http://bangumi.tv' + role_link[0]
                    # print(role_link)
                    role_html = self.handle_speical_url(role_link)
                    if role_html is not '':
                        role_position = role_html.xpath('//*[@id="columnCrtB"]//ul[@class="browserList"]/li')
                        if len(role_position) > 0:
                            for role in role_position:
                                role_item = dict()

                                # 1、角色的日文名字
                                role_item["role_japanese_name"] = role.xpath('./div[@class="ll innerLeftItem"]//h3/a/text()')[0] if len(role.xpath('./div[@class="ll innerLeftItem"]//h3/a/text()')) > 0 else ''
                                # print(role_item["role_japanese_name"])
                                # print(role_link)

                                # 2、角色的中文名字
                                # role_item["role_chinese_name"] = role.xpath('./div[@class="ll innerLeftItem"]//h3/p/text()')
                                role_item["role_chinese_name"] = role.xpath('.//p/text()')[0] if len(role.xpath('.//p/text()')) > 0 else ''
                                # print(role_item["role_chinese_name"])
                                # print(role_link)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值