日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php获取html中文本框内容_小猿圈Python入门之批量获取html内body内容的方法

發(fā)布時間:2024/7/23 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php获取html中文本框内容_小猿圈Python入门之批量获取html内body内容的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對于人工智能你了解有多少?你知道Python的使用嗎?這個對于大部分初學(xué)者來說都是很難的,小猿圈Python講師會每天為大家選擇分享一個知識點,希望對你學(xué)習(xí)Python有所幫助,今天分享的就是批量獲取html內(nèi)body內(nèi)容的方法。

現(xiàn)在有一批完整的關(guān)于介紹城市美食、景點等的html頁面,需要將里面body的內(nèi)容提取出來。

方法:利用python插件beautifulSoup獲取htmlbody標(biāo)簽的內(nèi)容,并批量處理。

# -*- coding:utf8 -*-

from bs4 import BeautifulSoup

import os

import os.path

import sys

reload(sys)

sys.setdefaultencoding('utf8')

def printPath(level,path):

global allFileNum

#所有文件夾,第一個字段是此目錄的級別

dirList = []

#所有文件

fileList = []

#返回一個列表,其中包含在目錄條目的名稱

files = os.listdir(path)

#先添加目錄級別

dirList.append(str(level))

for f in files:

if(os.path.isdir(path+'/'+f)):

#排除隱藏文件夾,因為隱藏文件夾過多

if(f[0] == '.'):

pass

else:

#添加隱藏文件夾

dirList.append(f)

if(os.path.isfile(path+'/'+f)):

#添加文件

fileList.append(f)

return (dirList,fileList)

#將文件html文件抓取并寫入指定txt文件

def getAndInsert(rootdir,savepath,path):

global file_num

f_list = os.listdir(rootdir+'/'+path)

for i in f_list:

temp = os.path.splitext(i)[0]

for num in range(1,11):

if(i==str(num)+'.html'):

#print rootdir+'/'+path+'/'+i

objFile = open(rootdir+'/'+path+'/'+i)

soup = BeautifulSoup(objFile)

arr = []

for child in soup.body:

arr.append(child)

if os.path.exists(savepath+'/'+path):

pass

else:

os.makedirs(savepath+'/'+path)

f = open(savepath+'/'+path+'/'+temp+'.txt','w')

for k,v in enumerate(arr):

if k!=1:

f.write(str(v))

f.close()

print path+'/'+i+' is running'

file_num = file_num + 1

rootdir = '../zips2'

dirList,fileList = printPath(1,rootdir)

savepath = "../testC"

file_num = 0

for fn in dirList:

if(fn == '1'):

pass

else:

getAndInsert(rootdir,savepath,fn)

print fn+' is ending'

print '一共完成'+str(file_num)+'個城市的提取'

最后想要了解更多關(guān)于Python和人工智能方面內(nèi)容的小伙伴,請關(guān)注小猿圈官網(wǎng),IT職業(yè)在線學(xué)習(xí)教育平臺為您提供Python開發(fā)環(huán)境搭建視頻等等,希望在你自學(xué)python中幫助你指導(dǎo)方向,少走彎路。

總結(jié)

以上是生活随笔為你收集整理的php获取html中文本框内容_小猿圈Python入门之批量获取html内body内容的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。