Python識別html主要文本框過程解析
這篇文章主要介紹了python識別html主要文本框過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
在抓取網(wǎng)頁的時候只想抓取主要的文本框,例如 csdn 中的主要文本框為下圖紅色框:

抓取的思想是,利用 bs4 查找所有的 div,用正則篩選出每個 div 里面的中文,找到中文字數(shù)最多的 div 就是屬于正文的 div 了。定義一個抓取的頭部抓取網(wǎng)頁內(nèi)容:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
'Host': 'blog.csdn.net'}
session = requests.session()
def getHtmlByRequests(url):
headers.update(
dict(Referer=url, Accept="*/*", Connection="keep-alive"))
htmlContent = session.get(url=url, headers=headers).content
return htmlContent.decode("utf-8", "ignore")
識別每個 div 中文字的正則:
import re # 統(tǒng)計中文字數(shù) def countContent(string): pattern = re.compile(u'[\u1100-\uFFFD]+?') content = pattern.findall(string) return content
遍歷每一個 div ,利用正則判斷里面中文的字數(shù)長度,找到長度最長的 div :
# 分析頁面信息
def analyzeHtml(html):
# 初始化網(wǎng)頁
soup = BeautifulSoup(html, "html.parser")
part = soup.select('div')
match = ""
for paragraph in part:
content = countContent(str(paragraph))
if len(content) > len(match):
match = str(paragraph)
return match
得到主要的 div 后,提取里面的文字出來:
def main():
url = "http://blog.csdn.net/"
html = getHtmlByRequests(url)
mainContent = analyzeHtml(html)
soup = BeautifulSoup(mainContent, "html.parser")
print(soup.select('div')[0].text)
完整的代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import re
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
'Host': 'blog.csdn.net'}
session = requests.session()
def getHtmlByRequests(url):
headers.update(
dict(Referer=url, Accept="*/*", Connection="keep-alive"))
htmlContent = session.get(url=url, headers=headers).content
return htmlContent.decode("utf-8", "ignore")
# 統(tǒng)計中文字數(shù)
def countContent(string):
pattern = re.compile(u'[\u1100-\uFFFD]+?')
content = pattern.findall(string)
return content
# 分析頁面信息
def analyzeHtml(html):
# 初始化網(wǎng)頁
soup = BeautifulSoup(html, "html.parser")
part = soup.select('div')
match = ""
for paragraph in part:
content = countContent(str(paragraph))
if len(content) > len(match):
match = str(paragraph)
return match
def main():
url = "http://blog.csdn.net/"
html = getHtmlByRequests(url)
mainContent = analyzeHtml(html)
soup = BeautifulSoup(mainContent, "html.parser")
print(soup.select('div')[0].text)
if __name__ == '__main__':
main()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python使用get_text()方法從大段html中提取文本的實例
- python爬蟲入門教程--HTML文本的解析庫BeautifulSoup(四)
- Python轉(zhuǎn)換HTML到Text純文本的方法
- python如何發(fā)送帶有附件、正文為HTML的郵件
- python中HTMLParser模塊知識點總結(jié)
- python郵件中附加文字、html、圖片、附件實現(xiàn)方法
- python 將html轉(zhuǎn)換為pdf的幾種方法
- python爬蟲beautifulsoup解析html方法
- python爬蟲 requests-html的使用
- 關(guān)于pycharm 切換 python3.9 報錯 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的問題
- Python HTMLTestRunner如何下載生成報告
- python 提取html文本的方法
相關(guān)文章
python中的iterator和"lazy?iterator"區(qū)別介紹
這篇文章主要介紹了python中的iterator和?“l(fā)azy?iterator“之間有什么區(qū)別,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
python利用wx實現(xiàn)界面按鈕和按鈕監(jiān)聽和字體改變的方法
今天小編就為大家分享一篇python利用wx實現(xiàn)界面按鈕和按鈕監(jiān)聽和字體改變的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
關(guān)于Python中兩個不同shape的數(shù)組間運算規(guī)則
這篇文章主要介紹了關(guān)于Python中兩個不同shape的數(shù)組間運算規(guī)則,眾所周知,相同?shape?的兩個數(shù)組間運算是指兩個數(shù)組的對應(yīng)元素相加,我們經(jīng)常會碰到一些不同?shape?的數(shù)組間運算,需要的朋友可以參考下2023-08-08
python解決報錯ImportError: Bad git executable.問題
這篇文章主要介紹了python解決報錯ImportError: Bad git executable.問題。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

