MCPcopy Create free account
hub / github.com/dataabc/weiboSpider / CommentParser

Class CommentParser

weibo_spider/parser/comment_parser.py:15–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15class CommentParser(Parser):
16 def __init__(self, cookie, weibo_id):
17 self.cookie = cookie
18 self.url = 'https://weibo.cn/comment/' + weibo_id
19 self.selector = handle_html(self.cookie, self.url)
20
21 def get_long_weibo(self):
22 """获取长原创微博"""
23 try:
24 for i in range(5):
25 self.selector = handle_html(self.cookie, self.url)
26 if self.selector is not None:
27 info_div = self.selector.xpath("//div[@class='c' and @id='M_']")[0]
28 info_span = info_div.xpath("//span[@class='ctt']")[0]
29 # 1. 获取 info_span 中的所有 HTML 代码作为字符串
30 html_string = etree.tostring(info_span, encoding='unicode', method='html')
31 # 2. 将 <br> 替换为 \n
32 html_string = html_string.replace('<br>', '\n')
33 # 3. 去掉所有 HTML 标签,但保留标签内的有效文本
34 new_content = fromstring(html_string).text_content()
35 # 4. 替换多个连续的 \n 为一个 \n
36 new_content = re.sub(r'\n+\s*', '\n', new_content)
37 weibo_content = handle_garbled(new_content)
38 if weibo_content is not None:
39 return weibo_content
40 sleep(random.randint(6, 10))
41 except Exception:
42 logger.exception(u'网络出错')
43
44 def get_long_retweet(self):
45 """获取长转发微博"""
46 return self.get_long_weibo()
47
48 def get_video_page_url(self):
49 """获取微博视频页面的链接"""
50 video_url = ''
51 try:
52 self.selector = handle_html(self.cookie, self.url)
53 if self.selector is not None:
54 # 来自微博视频号的格式与普通格式不一致,不加 span 层级
55 links = self.selector.xpath("body/div[@class='c' and @id][1]/div//a")
56 for a in links:
57 if 'm.weibo.cn/s/video/show?object_id=' in a.xpath(
58 '@href')[0]:
59 video_url = a.xpath('@href')[0]
60 break
61 except Exception:
62 logger.exception(u'网络出错')
63
64 return video_url

Callers 4

test_comment_parserFunction · 0.90
get_original_weiboMethod · 0.85
get_retweetMethod · 0.85
get_video_urlMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_comment_parserFunction · 0.72