bs4 python解析html
生活随笔
收集整理的這篇文章主要介紹了
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 iodef 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
轉載于:https://www.cnblogs.com/MyNameIsMT/p/5426664.html
總結
以上是生活随笔為你收集整理的bs4 python解析html的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL 数据库 练习题
- 下一篇: websocket python爬虫_p