简单的python2.7基于bs4和requests的爬虫

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.......")

 

你可能感兴趣的:(简单的python2.7基于bs4和requests的爬虫)