喜欢折腾的我又换了评论系统,虽然之前的valine一直用的很舒服,奈何人都是善变的,何况一个喜欢折腾的人。这次换的是disqus评论系统,我垂涎已久的一个评论系统,终于可以换上了。
不过有个问题就是之前valine的评论怎么搬过来?先不自己造轮子的原则,我在网上搜了一遍,没有,全是“多说换到disqus的轮子”,完全没有valine换到disqus的,而且还有一批从disqus转到vlaie的,看来valine还是很香的。奈何我还是要跑,disqus更想哈哈。
然后就是自己琢磨怎么换,disuqs有个导入评论的页面https://import.disqus.com/ ,可以从这个入口导入评论,但是支持的格式是wordpress
的xml
文件格式,它的文件格式如下,所以我要写一个程序把valine导出的评论转为下面的格式
wordpress的xml格式长什么样
下面各个标签的内容有点前端基础的朋友看下应该就知道里面应该填什么内容。一个item
表示一篇文章,一篇文章里面有多个评论,也就是一个item
里面可以有多个<wp:comment>
标签
<?xml version="1.0" encoding="gb2312"?> <rss version ="2.0" xmlns:content ="http://purl.org/rss/1.0/modules/content/" xmlns:dsq ="http://www.disqus.com/" xmlns:dc ="http://purl.org/dc/elements/1.1/" xmlns:wp ="http://wordpress.org/export/1.0/" > <channel > <item > <title > 文章标题</title > <link > https://flycode.co/post/46f19c88.html</link > <content:encoded > <![CDATA[ 文章内容 ]]></content:encoded > <wp:post_date_gmt > 2018-09-20 09:13:44</wp:post_date_gmt > <wp:comment_status > open</wp:comment_status > <wp:comment > <wp:comment_id > 1</wp:comment_id > <wp:comment_author > Anonymous</wp:comment_author > <wp:comment_author_email > 237199971@qq.com</wp:comment_author_email > <wp:comment_author_url > /images/blog/vultr_gg.jpg</wp:comment_author_url > <wp:comment_author_IP > 93.48.67.119</wp:comment_author_IP > <wp:comment_date_gmt > 2019-04-24 11:30:19</wp:comment_date_gmt > <wp:comment_content > <![CDATA[评论内容]]></wp:comment_content > <wp:comment_approved > 1</wp:comment_approved > <wp:comment_parent > 0</wp:comment_parent > </wp:comment > </item > </rss >
存放评论的txt格式
那么格式有了,就是写脚本了。我先从valine后台将评论都复制到一个txt文件里了,由于后面脚本读取文本内容的先后顺序因素,这个txt文本的排版有点要求,格式如下
评论者昵称 评论内容 文章链接 评论时间 Anonymous <p>夜间模式不支持这功能,</p> /post/46f19c88.html 2019-04-24 11:30:19 跨境电商之家 ADONCN.COM <p>感谢分享!</p> /post/651cfd47.html 2019-04-24 07:51:22 Anonymous <hr> <p>md语法也可以<del>好方便</del></p> /post/2d5da13e.html 2019-04-21 13:22:52
这是从valine里面复制出来的一些有用信息,但是与上面xml格式要求的内容还缺了点东西,所以脚本中还会用到爬虫来爬取文章标题与内容等。
所以数据也有了,要求的输出格式也有了,就差一个脚本了,一个简单的输入输出问题,输入一个txt文本,输出一个wordpress版本的xml文件
tip
当然,这个脚本你不能直接拿去用了,如果你也想用的话,需要做几件事
将要导入的评论写入上面我说的哪个格式的txt文本中
将下面代码爬虫部分的域名改为自己的域名
确保txt文本中的文章链接是uri格式,即不带全域名,否则你应该在下面脚本中去到域名
注意文本的编码格式,我这里用的是gb2312,你根据自己情况而定
导出xml文件后,先在浏览器中打开,如果可以打开的话,就可以导入了,导入
from bs4 import BeautifulSoupimport requestsr = open("comment.txt" , "r" ) lines = r.readlines() arr = [] for l in lines: if l != "
" : arr.append(l.replace("
" ,"" )) tmp = 4 tt, rel = [], [] for a in arr: if (tmp > 0 ): tt.append(a) tmp -= 1 else : rel.append(tt) tt = [] tt.append(a) tmp = 3 con = {} i = 0 for r in rel: if r[2 ] in con: pass else : con[r[2 ]] = {} con[r[2 ]][item ] = [] item = [] item.append(r[0 ]) item.append(r[1 ]) item.append(r[3 ]) con[r[2 ]][item ].append(item) // 爬虫部分 req = requests.get(url = "http://flycode.co" + r[2 ]) html = req.text div_bf = BeautifulSoup(html,"html.parser" ) title = "" try : title = div_bf.h2.string except : title = "一篇水文" body = "" try : body = div_bf.p.string if body == None : body = title except : body = "一篇水文" con[r[2 ]][title ] = str(title) con[r[2 ]][body ] = body con[r[2 ]][link ] = r[2 ] print(正在收集评论 + str(i)) i += 1 i = 0 xml = open(disqus.xml , "w" ) begin = """<?xml version="1.0" encoding="gb2312"?> <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dsq="http://www.disqus.com/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.0/" > <channel> """ xml.writelines(begin + "
" ) for k in con.keys(): c = con[k] item = c[item ] xml.writelines(<item>
) xml.writelines(<title> + str(c[title ]) + </title>
) xml.writelines(<link>https://flycode.co + c[link ] + </link>
) xml.write(<content:encoded><![CDATA[ + str(c[body ]) + ]]></content:encoded>
) xml.writelines(<wp:post_date_gmt>2018-09-20 09:13:44</wp:post_date_gmt>
) xml.writelines(<wp:comment_status>open</wp:comment_status>
) comid = 1 for com in item: xml.writelines(<wp:comment>
) xml.writelines( <wp:comment_id> + str(comid) + </wp:comment_id>
) xml.writelines( <wp:comment_author> + com[0 ] + </wp:comment_author>
) xml.writelines( <wp:comment_author_email>23719997 + str(comid) + @qq.com</wp:comment_author_email>
) xml.writelines( <wp:comment_author_url>/images/blog/vultr_gg.jpg</wp:comment_author_url>
) xml.writelines( <wp:comment_author_IP>93.48.67.119</wp:comment_author_IP>
) xml.writelines( <wp:comment_date_gmt> + com[2 ] + </wp:comment_date_gmt>
) xml.writelines( <wp:comment_content><![CDATA[ + com[1 ] + ]]></wp:comment_content>
) xml.writelines( <wp:comment_approved>1</wp:comment_approved>
) xml.writelines( <wp:comment_parent>0</wp:comment_parent>
) xml.writelines(</wp:comment>
) comid += 1 i += 1 print(正在写入评论 + str(i)) xml.writelines(</item>
) xml.writelines(</channel>
</rss> )
代码大概就是这样,将txt文本内容读出后,循环将评论加入文章字典中,以文章链接作为字典的key
,判断文章是否出现过,如果出现过就加入到文章里,没出现过就生成新的key,爬虫部分简单爬取了文章标题和内容,建议会的朋友根据自己博客页面代码改下爬虫,因为我不能确保你的博客标题和内容tag
名字和我的一样
源码就是提供了一个思路,不建议直接用,虽然我自己用的好好的,但是写的比较懒,没有很强的容错率,因为环境不同可能会出很多bug,所以你可以读取源码和我的几点提示后,自己将其进行更改。
按道理说只要通过简单地更改我的源码,你可以将所有平台的评论导入disqus评论系统。