原因:代码段换行符失效,所有代码一行显示
解决方法:使用Chrome浏览器即可解决(使用Microsoft Edge会出现此问题)
效果展示:
# _*_ coding:utf-8 _*_
import requests
from lxml import etree
import pymongo
client = pymongo.MongoClient('localhost',27017)
mydb = client['mydb']
timeline = mydb['timeline']
def get_time_info(url, page):
user_id = url.split('/')
user_id =user_id[4]
if url.find('page='):
page = page + 1
html = requests.get(url)
selector = etree.HTML(html.text)
infos = selector.xpath('//ul[@class="note-list"]/li')
for info in infos:
dd = info.xpath('div/div/div/span/@data-datetime')[0]
type = info.xpath('div/div/div/span/@data-type')[0]
timeline.insert_one({'date':dd, 'type':type})
id_infos = selector.xpath('//ul[@class="note-list"]/li/@id')
if len(id_infos) >1:
feed_id = id_infos[-1]
max_id = feed_id.split('-')[1]
next_url = 'https://www.jianshu.com/users/%s/timeline?max_id=%s&page=%s' %(user_id, max_id, page)
get_time_info(next_url, page)
if __name__ =='__main__':
get_time_info('https://www.jianshu.com/users/9104ebf5e177/timeline', 1)