從元素層面來說,即使是不在清單上的玩家 QUOTE,應該也可以做到遮蔽的
因為是在回應的元素集下的 <div class='quotetop'> 元素內,然後這元素內有 User-ID 可以做索引條件
我自己的爬蟲是先把所有 post-id 拉出來,然後針對各 post-id 的 post-user 做解析對應
隨後按照 post-id 去做內文讀取,然後存進陣列中
應該是可以追加這兩個動作
1.內文的 quote 解析,然後把特定字抹掉
2.指定 post-user 的 post-id 直接抹掉
如果是 python 我還可以努力一下, javascript 我只能嘴砲了
我的 python 爬蟲的解析段如下,有用的話可以拿去用
應該是針對 response_content 做回應加工,以及 user_id 做遮蔽動作
CODE
def Forums_Respond_Segmentation(soup):
response_info_array = {}
# 找到所有具有 class="borderwrap" 的 div 元素
borderwrap_divs = soup.find_all('div', class_='borderwrap')
# 遍歷每個<div class="borderwrap">
for div in borderwrap_divs:
# 提取序列編號
post_number_tag = div.find(
'a', onclick=lambda x: x and 'link_to_post' in x)
if post_number_tag:
# 取得 post 序號(#多少)
post_number = post_number_tag.text.strip('#')
# 初始化 post_number 的子字典
response_info_array[post_number] = {}
# 透過擷取 <a herf>,取得 post-id
a_tag = div.find('a')
onclick_value = a_tag['onclick']
post_id_start = onclick_value.find('(') + 1
post_id_end = onclick_value.find(')')
post_id = onclick_value[post_id_start:post_id_end]
# 提取 user-id 和 user-uid
user_span = div.find('span', class_='bigusername')
user_id = user_span.find('a').text
user_uid = user_span.find('a')['href'].split('=')[-1]
response_content = div.find(
'div', class_='postcolor').text.strip()
# 判斷編輯狀態
post_edited_ststus = 'This post has been edited by' in response_content
# 如果已經編輯過,複製編輯資訊至 post_edited_info,並在 response_content 移除相應字串
post_edited_info = 'Null'
if post_edited_ststus:
edited_start = response_content.find(
'This post has been edited by')
post_edited_info = response_content[edited_start:].strip()
response_content = response_content[:edited_start].strip()
response_info_array[post_number]['Post-ID'] = post_id
response_info_array[post_number]['User-ID'] = user_id
response_info_array[post_number]['User-UID'] = user_uid
response_info_array[post_number]['response'] = response_content
response_info_array[post_number]['post_edited_ststus'] = post_edited_ststus
response_info_array[post_number]['post_edited_info'] = post_edited_info
return response_info_array