[Python] 纯文本查看 复制代码 import requests
from lxml import html
etree = html.etree
class Discuz:
def parse(self):
for i in range(1,5):
url = 'http://8.142.187.40/forum.php?mod=viewthread&tid={}'.format(i)
r = requests.get(url)
html = r.text
if r.status_code == 200:
if "抱歉,指定的主题不存在或已被删除或正在被审核" in html:
continue
else:
resp = requests.get(url).text
tree = etree.HTML(resp)
tid = i
name = tree.xpath('//a[contains(@class,"xw1")]/text()')[2]
score = tree.xpath('//td/p/a/text()')[0]
level = tree.xpath('//p[1]/em/a/text()')[0]
title = tree.xpath('//span[contains(@id,"thread")]/text()')[0]
contont = tree.xpath('//td[contains(@class,"t_f")]/text()')[0]
print("帖子ID","用户名","积分","等级","标题","内容")
# print(tid,name,score,level,title,contont)
# with open('D:\\1.txt','a+',encoding='utf-8')as f:
# f.write(str(tid)+','+name+','+str(score)+','+level+','+title+','+contont.strip()+"\n")
if __name__ == '__main__':
d = Discuz()
d.parse()
当前代码仅用于学习,用Python+Xpath实现
写的不好大家不要喷我,谢谢,我是小趴菜。 |