锐哥@ 2023-03-13 17:09 采纳率: 80%
浏览 45
已结题

关于xpath爬虫遇到的问题

请问各位,用xpath提取评语(比如图片中标黄的:微信读书,推荐回想剧场有声响)怎么提取呀,本来(759548人评价)也不会的后来百度解决了,这第二个问题实在不会,麻烦各位优秀的程序员(媛)解答谢谢,谢谢

img

img

结果

img


"""
豆瓣图书top250数据抓取-lxml+xpath
"""

import requests
from lxml import etree
import time
import random
from  fake_useragent import UserAgent

class DoubanBookSpider:
    def __init__(self):
        self.url='https://www.douban.com/doulist/139873963/?start={}'


    def get_html(self,url):
        """请求函数-获取html"""
        headers={"User-Agent":UserAgent().random}
        html=requests.get(url=url,headers=headers).content.decode('utf-8','ignore')
        # 直接调用解析函数
        self.parse_html(html)

    def parse_html(self, html):
        """解析函数-解析提取数据"""
        parse=etree.HTML(html)
        table_list=parse.xpath('//div[@class="article"]')
        for table in table_list:
            item={}
            # item["name"]=table.xpath('.//div[@class="title"]/a/text()')
            # # print(item["name"])
            # item["comment"]=table.xpath('.//div[@class="abstract"]/text()')
            # # print(item["comment"])
            # item["score"]=table.xpath('.//span[@class="rating_nums"]/text()')
            # # print(item["score"])
            # item["number"]=table.xpath('.//div[@class="rating"]/span/text()')
            # # print(item["number"])
            # item["instructions"] = table.xpath('.//blockquote[@class="comment"]/text()')
            # # print(item["instructions"])
            # print(item)

             # 书的名称
            name_list=table.xpath('.//div[@class="title"]/a/text()')
            item["name"]=name_list[0].strip() if name_list else None
            # 以上相当于x=3 if 5>2 else 8

            # 书的描述
            comment_list=table.xpath('.//div[@class="abstract"]/text()')
            item["comment"] = comment_list[0].strip() if name_list else None

            # 书的评分
            comment_list=table.xpath('.//span[@class="rating_nums"]/text()')
            item["score"] = comment_list[0].strip() if name_list else None

            # 评论人数
            number_list=table.xpath('.//div[@class="rating"]/span[count(@*)=0]/text()')
            item["number"]= number_list[2].strip() if name_list else None

            # 评语
            instructions_list = table.xpath('.//blockquote[@class="comment"]/span/text()')
            item["instructions"]= instructions_list[0].strip() if name_list else None
            print(item)


    def run(self):
        for page in range(1,11):
            start=(page-1)*25
            page_url=self.url.format(start)
            self.get_html(url=page_url)
            # 控制数据抓取的频率
            time.sleep(random.uniform(0,2))

if __name__ == '__main__':
    spider=DoubanBookSpider()
    spider.run()





  • 写回答

5条回答 默认 最新

  • cjh4312 2023-03-13 18:05
    关注
    
    titles=html.xpath('//*[@class="title"]/a//text()')
    abstracts=html.xpath('//*[@class="abstract"]')
    ratings=html.xpath('//*[@class="rating"]')
    comments=html.xpath('//*[@class="comment"]')
    for n,i in enumerate(titles):
        print(f'{n+1}.{i.strip()}')
        abstarct=abstracts[n].xpath('.//text()')
        for j in abstarct:
            print(j.strip())
        rating=ratings[n].xpath('.//text()')
        for k in rating:
            print(k.strip())
        comment=comments[n].xpath('.//text()')
        for m in comment:
            print(m.strip())
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日
  • 创建了问题 3月13日

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真