使用文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

python的编码问题比较恶心。

decode解码
encode编码


在文件头设置
# -*- coding: utf-8 -*-
让python使用utf8.




# -*- coding: utf-8 -*-
__author__ = 'Administrator'

from bs4 import BeautifulSoup
import requests
import os
import sys
import io

def getHtml(url):
    r = requests.get(url)
    content = r.content.decode('utf8')
    #print(content)
    soup = BeautifulSoup(content)
    print(soup.find_all('h2'))
    print(soup.find_all('p'))

if __name__=="__main__":

    print(sys.getdefaultencoding())
    print("start.......")
    url = "http://www.jiakaobaodian.com/mnks/exercise/0-c1-kemu1-chengdu.html?id=800000"
    getHtml(url)
    print("end.......")

  Demo

posted on 2016-04-24 11:54  李祥  阅读(5748)  评论(0编辑  收藏  举报