bs4 python解析html


使用文档: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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM