python获取已打开网页的html,【已解决】Python的BeautifulSoup去实现提取带tag的HTML网页主体内容...
折騰:
【未解決】Python的html網頁主體內容提取
期間,去試試BeautifulSoup提取HTML網頁主體內容
先去隨便找個合適的網頁
-》
簡單看了看網頁內容結構:
發現是:
網頁主體內容是://*[@id="articleContent"]
/html/body/div[4]/div[1]/div[1]/div[4]
這部分其下的內容,才是新聞主體內容。
此處暫時就模擬獲取articleContent其下的內容,視為文章主體內容
然后用代碼:# Function: Use BeautifulSoup to extract body content of HTML web page
# Author: Crifan
# Update: 20200721
import os
import codecs
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
import requests
################################################################################
# Config
################################################################################
CurrentFolder = os.getcwd()
CurrentFolder = os.path.abspath(CurrentFolder)
OutputRoot = os.path.join(CurrentFolder, "HtmlExtract", "output")
################################################################################
# Util Functions
################################################################################
def datetimeToStr(inputDatetime, format="%Y%m%d_%H%M%S"):
"""Convert datetime to string
Args:
inputDatetime (datetime): datetime value
Returns:
str
Raises:
Examples:
datetime.datetime(2020, 4, 21, 15, 44, 13, 2000) -> '20200421_154413'
"""
datetimeStr = inputDatetime.strftime(format=format)
# print("inputDatetime=%s -> datetimeStr=%s" % (inputDatetime, datetimeStr)) # 2020-04-21 15:08:59.787623
return datetimeStr
def getCurDatetimeStr(outputFormat="%Y%m%d_%H%M%S"):
"""
get current datetime then format to string
eg:
20171111_220722
:param outputFormat: datetime output format
:return: current datetime formatted string
"""
curDatetime = datetime.now() # 2017-11-11 22:07:22.705101
# curDatetimeStr = curDatetime.strftime(format=outputFormat) #'20171111_220722'
curDatetimeStr = datetimeToStr(curDatetime)
return curDatetimeStr
def saveTextToFile(fullFilename, text, fileEncoding="utf-8"):
"""save text content into file"""
with codecs.open(fullFilename, 'w', encoding=fileEncoding) as fp:
fp.write(text)
fp.close()
################################################################################
# Main
################################################################################
# 【圖】超級播報:特斯拉市值背后的技術趨勢_汽車之家
postUrl = "https://www.autohome.com.cn/news/202007/1014507.html?pvareaid=3311314"
resp = requests.get(postUrl)
respHtml = resp.text
# soup = BeautifulSoup(respHtml)
soup = BeautifulSoup(respHtml, "html.parser")
postHtml = str(soup.html)
# print("postHtml=%s" % postHtml)
curDatetimeStr = getCurDatetimeStr()
saveHtmlFileName = "html_%s.html" % curDatetimeStr
saveHtmlFilePath = os.path.join(OutputRoot, saveHtmlFileName)
saveTextToFile(saveHtmlFilePath, postHtml)
就保存出對應html文件了:
然后去打開看看效果,結果:
即:
【已解決】BeautifulSoup導出的網頁內容中圖片無法顯示原因是把前綴https:變成file:了
至此,算是內容上,就基本OK了。
對于提取主體內容,即:
的://*[@id="articleContent"]
和此處
的:article
就以后再折騰。
【后記20200722】
再去折騰,提取主體內容,此處暫時用手動寫規則的辦法。
借用Chrome去復制出xpath:
//*[@id="content_bit"]/article
不過此處發現article只有一處:
所以為了簡單起見,直接用article也就可以了。
不過其中遇到:
【已解決】BeautifulSoup中find得到的soup節點如何獲取自身及其下子孫節點的html源碼
繼續
總結
以上是生活随笔為你收集整理的python获取已打开网页的html,【已解决】Python的BeautifulSoup去实现提取带tag的HTML网页主体内容...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7网络的计算机名称,Win7指定的
- 下一篇: win8计算机usb无法识别usb设备,