SeverChan_Nyedu
Crawl the infomation about competitons, when new infos comes, remind people on Wechat by ServerChan:
完成笔记:
lxml
中etree.xpath()
对于tbody
的处理
1.关于该网页通过
table
对页面进行分布设置,其中table
标签会自动生成tbody
标签,如图..此时用xpath
进行匹配的时候就不需要将tbody
加上,否则匹配不到
布局<Table>
表格<table>
可以看到的是在Chrome调试助手里面,<table>
下面都是会自动生成<tbody>
标签的,而我们再通过网页源码看看..==>可以发现的是:
<table>
布局是没有<tbody>
的,只有表格才有,所以这也是为什么用xpath()表格里必须加上tbody
才能匹配,而table
布局中不能加tbody
的原因
#布局获得内容
for content in contentList:
title = content.xpath('td/table/tr/td[1]/a/text()')[0]
href = content.xpath('td/table/tr/td[1]/a/@href')[0]
submittime = content.xpath('td/table/tr/td[2]/div/text()')[0]
#表格
if content.tag == 'table':
tabletitle = content.xpath('tbody/tr[1]/td') # 表格头
tabletitleList = map(lambda x: x.xpath('string(.)'), tabletitle)
tablehead = '|' + '|'.join(tabletitleList) + '|'
tableover = '|' + ':---:|' * len(tabletitle) # 居中显示
requests.get()
获得的response的编码问题
2.html = requests.get('http://jwc.njupt.edu.cn/1594/list.htm',headers = headers)
html.encoding = 'utf-8'
#Requests库的自身编码为: r.encoding = ‘ISO-8859-1’
TODO:
- 专门写个获得内容的函数,方便转发给多人
- 将超链接也趴下来
更新
- 2019-6-10 没有接收到信息,21号查看时发现
//*[@id="newslist"]/div/table/tr
变成了//*[@id="newslist"]/div/div/table/tr