首页 > 编程语言 > 详细

Python学习

时间:2017-03-31 22:24:14      阅读:240      评论:0      收藏:0      [点我收藏+]


#!/usr/bin/python
#coding=utf8
from bs4 import BeautifulSoup
import json
import requests
import itchat
from itchat.content import *
def page(url):
    return  BeautifulSoup(requests.get(url).text,‘lxml‘)
#from ipdb import set_trace
#set_trace()
def got_hot_article(items):
    for item in items:
        if item.h2.text == ‘热门文章‘:
            return item.ul.find_all(‘li‘)
def parse(bs_data:BeautifulSoup):
    return got_hot_article(bs_data.find_all(‘div‘,attrs={‘class‘:‘mps‘}))
def format_data(items):
    result = []
    for item in items:
        info = {}
        info[‘title‘] = item.a.text
        info[‘url‘] = ‘http://www.ftchinese.com‘ + item.a.get(‘href‘)
        result.append(info)
    return json.dumps(result, indent=4, ensure_ascii=False)
def main():
    print(format_data(parse(page(‘http://www.ftchinese.com‘))))
try:
    if __name__ == ‘__main__‘:
        main()
except HttpError as e:
    print(e)



Python学习

原文:http://yanruohan.blog.51cto.com/9740053/1912139

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!