亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 python 遍歷 scrapy 中的 XML 子節點?

如何使用 python 遍歷 scrapy 中的 XML 子節點?

不負相思意 2022-12-20 15:30:19
我想抓取此頁面上的評論,但我似乎無法了解如何遍歷包含評論的節點的子節點并獲取數據點。這是 hmtl 的一部分:        <div class="comment">            <div class="comment-user">                <div class="comment-user-avatar">                    <a href="https://www.picuki.com/profile/alexandera_300">                        <img src="https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-19/s150x150/98342975_2815537605343770_6875611169034338304_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&amp;_nc_ohc=VjMtcOxXuaQAX_ZCqee&amp;oh=4cf78fecbadcb57a81672c6edecc15a2&amp;oe=5F02D580" alt="alexandera_300">                    </a>                </div>                <div class="comment-user-nickname">                    <a href="https://www.picuki.com/profile/alexandera_300">@alexandera_300</a>                </div>            </div>            <div class="comment-text">                #followforfollowback            </div>        </div>        <div class="comment">            <div class="comment-user">                <div class="comment-user-avatar">                    <a href="https://www.picuki.com/profile/coxlogan2008">                        <img src="https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-19/s150x150/101229634_275138197009045_1475918829270859776_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&amp;_nc_ohc=e4gTZqQGpEAAX_7U-Q0&amp;oh=36b7f5d1a0d7069f2447f4a318edec7d&amp;oe=5F004A54" alt="coxlogan2008">                    </a>                </div>                <div class="comment-user-nickname">                    <a href="https://www.picuki.com/profile/coxlogan2008">@coxlogan2008</a>                </div>            </div>            <div class="comment-text">                ??            </div>        </div>我正在使用的 python 代碼片段是這樣的:    def parse_post(self, response):    img_url = response.meta['img_url']    caption = response.meta['caption']但是,當我運行它時,我只獲得了第n條評論的數據。有人可以幫我嗎?我不明白為什么代碼不遍歷節點。提前致謝!
查看完整描述

1 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

我認為您的問題來自您的“評論”的 xpath。通過僅獲取文本,您不會選擇節點。以下更改使其對我有用:


# the likes & number of comments only have to be taken once, should not be part of the loop

likes = response.xpath('.//span[@class="icon-thumbs-up-alt"]/text()').get()

num_of_comments = response.xpath('.//span[@id="commentsCount"]/text()').get()

comments = response.xpath('//div[@id="commantsPlace"]/*[@class="comment"]')

for comment in comments:  

    comment_user_name = comment.xpath('.//*[@class="comment-user-nickname"]/a/text()').get()

    comment_text = comment.xpath('.//*[@class="comment-text"]/text()').get()


查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號