如何使用 Selenium 及 Xpath 在论坛网站只抓取 xpath 路径的回复内容,而不抓取引用内容?

大家好,小弟正需要对以下网站进行网路爬虫,所使用的工具是Selenium、Web Driver (chromedriver)、Python
以下是目标爬虫网站:
https://forumd.hkgolden.com/view.aspx?type...
小弟正需要针对以下帖子的每一页抓取每一位用户回复的内容,但是部分的回复内容包含了引用内容(蓝色文字),我只想抓黑色问题的内容,目前试过很多方法都未能成功。
目前我是使用了以下代码抓取回复的内容(full xpath 的绝对路径)

reply_data = driver_blank.find_element_by_xpath("/html/body/form/div[5]/div/div/div[2]/div[1]/div[5]/table[8]/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[1]/td/div/text()")
print(reply_data)

以下图片是我需要抓取的例子,我要抓取黑色文字的内容,不要蓝色文字的内容
帖子回复内容
以下是回复内容的部分HTML代码:

<table class="repliers_right" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="vertical-align: top;">
<div class="ContentGrid">
<blockquote><div style="color: #0000A0;">有冇第隻款<img data-icons=":-[lm" src="/faces/lomore/angry.gif" alt=":-[lm"> <img data-icons=":-(lm" src="/faces/lomore/frown.gif" alt=":-(lm"> 我想要呢兩隻</div></blockquote><br>係囉,反應好既會唔會考慮出其他?<br>我都想要其他<img data-icons="^3^lm" src="/faces/lomore/kiss.gif" alt="^3^lm"> <img data-icons="[bomb]lm" src="/faces/lomore/bomb.gif" alt="[bomb]lm">
<br><br><br>
</div>
</td>
</tr>
<tr>
<td style="text-align: center; vertical-align: top;">
<div id="lineImage6" style="display: block; overflow: hidden;">
</div>
</td>
</tr>
<tr>
<td style="width: 100%; text-align: right;">
<div style="float: right; vertical-align: bottom; margin-top: 5px;">
<div id="lauming6" style="float: left; vertical-align: bottom;"></div>&nbsp;
<a class="btn btn_small btn_bookmark" href="Javascript:bookmarkThis(7219211)" id="laumingHref">留名</a>
<a class="btn btn_small btn_complain" href="contactus.aspx?messageid=7219211&amp;replyid=275220714">投訴文章</a>
<a class="btn btn_small btn_quote" href="Javascript:QuoteReply(7219211,275220714);">快速引用</a>
<a class="btn btn_small btn_quote" href="post.aspx?mt=Y&amp;rid=275220714&amp;id=7219211&amp;page=2">引用原文</a>
<span style="font-size: 12px; color:gray;">
15/4/2020 13:18
</span>
</div>
</td>
</tr>
</tbody></table>

HTML代码

我尝试过使用start-with、contains那些,但上网找到的一些都无法让我执行成功,各位能否协助我解决这个问题,谢谢你们~

讨论数量: 5

这种应该都是先抓取全部内容,后期处理过滤掉的。你如果抓取的时候这种都不抓,后期需求变动,要知道具体的回复内容回复的是那一条帖子的时候岂不是要从新抓取。

直接先全部抓取,后期展示时清洗或者过滤掉就可以,过滤用正则替换掉 blockquote 标签里面的内容即可。

4年前 评论

//div[@class="ContentGrid"]/text ()

4年前 评论

pyquery库可以删除标签

4年前 评论
Jason990420
from lxml import etree
from Tool import Read_URL
"""
Tool - https://learnku.com/articles/42671
"""
url = 'https://forumd.hkgolden.com/view.aspx?type=BW&message=7219211&page=2'
response, text = Read_URL(url)
html = etree.HTML(text)
tags = html.xpath('//div[@class="ContentGrid"]/text()')
result = '\n'.join([tag.strip() for tag in tags if tag.strip() != ''])
print(result)
4年前 评论
fd5556 (楼主) 4年前
Jason990420

你没看我里面的注解

博客:个人常用集

4年前 评论
fd5556 (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!