Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
最近工作需要使用python爬取一些網(wǎng)頁(yè)數(shù)據(jù),爬取網(wǎng)頁(yè)是非靜態(tài)頁(yè)面
1、只能爬取靜態(tài)頁(yè)面
2、seleium需要下注對(duì)應(yīng)版本的瀏覽器驅(qū)動(dòng),若是瀏覽器升級(jí),驅(qū)動(dòng)還得重新下載對(duì)應(yīng)版本的驅(qū)動(dòng)程序,pass
3、最終選擇的庫(kù),可以開(kāi)啟一個(gè)無(wú)界面瀏覽器,可以模擬瀏覽器打開(kāi)一個(gè)頁(yè)面,并輸入url最終加載指定頁(yè)面
1、安裝庫(kù)
# 模擬無(wú)界面瀏覽器
pip install pyppeteer
# 頁(yè)面解析
pip install beautifulsoup4
2、非靜態(tài)頁(yè)面爬取demo
# _*_ coding: utf-8 _*_
# @Time: 2023/12/26
# @TODO: 爬取非靜態(tài)頁(yè)面demo
# @Author: wkq
import asyncio
from pyppeteer import launch

async def fetch_page_content(url):
"""
todo 異步請(qǐng)求發(fā)送
:param url: 非靜態(tài)頁(yè)面url
:return: content頁(yè)面內(nèi)容
"""
# 啟動(dòng)無(wú)界面瀏覽器
browser = await launch(headless=True)
# 創(chuàng)建一個(gè)新的頁(yè)面
page = await browser.newPage()
# 訪問(wèn)目標(biāo)URL并等待頁(yè)面加載完成
await page.goto(url, {'waitUntil': 'networkidle2'})
# 獲取頁(yè)面的HTML內(nèi)容
content = await page.content()
await browser.close() # 關(guān)閉瀏覽器
return content
async def main():
"""

todo 異步執(zhí)行請(qǐng)求加載頁(yè)面,解析頁(yè)面
:return:
"""
# 非靜態(tài)頁(yè)面url
url = "https://www.sporttery.cn/jczx/jclq/ssjx/20231225/10039058.html"
html_content = await fetch_page_content(url)
# 使用BeautifulSoup解析HTML
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
# 打印整個(gè)網(wǎng)頁(yè)的HTML內(nèi)容
print(soup.prettify())
if __name__ == "__main__":
# 使用事件循環(huán)執(zhí)行異步方法
asyncio.get_event_loop().run_until_complete(main())
怎么在js里打開(kāi)文件
在里打開(kāi)文件的核心操作包括:使用File API讀取本地文件、通過(guò)fetch API獲取遠(yuǎn)程文件、以及通過(guò)Node.js的fs模塊處理服務(wù)器端文件。 在此,我們將詳細(xì)探討這三種方法中的一種:使用File API讀取本地文件。
一、使用File API讀取本地文件
1. 文件選擇器
要使用File API讀取本地文件,首先需要用戶(hù)選擇文件。通過(guò)HTML的標(biāo)簽,可以創(chuàng)建一個(gè)文件選擇器。
2. 讀取文件內(nèi)容
接下來(lái),通過(guò)監(jiān)聽(tīng)文件選擇事件,并使用讀取文件內(nèi)容。
document.getElementById('fileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result); // 讀取的文件內(nèi)容
};
reader.readAsText(file); // 以文本形式讀取文件
}
});
對(duì)象 提供了幾種不同的方法來(lái)讀取文件內(nèi)容,例如 , , 和 。其中 方法最常用于讀取文本文件。
二、使用fetch API獲取遠(yuǎn)程文件
1. 基本用法
fetch API可以用于獲取遠(yuǎn)程服務(wù)器上的文件內(nèi)容。以下是一個(gè)示例:
fetch('https://example.com/file.txt')
.then(response => response.text())
.then(data => {
console.log(data); // 遠(yuǎn)程文件內(nèi)容
})
.catch(error => {
console.error('Error fetching the file:', error);
});
2. 錯(cuò)誤處理
使用fetch API時(shí),處理錯(cuò)誤非常重要。可以通過(guò)catch方法捕獲并處理異常情況。
fetch('https://example.com/file.txt')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
三、使用Node.js的fs模塊處理服務(wù)器端文件
1. 導(dǎo)入fs模塊
在Node.js環(huán)境中,可以使用內(nèi)置的fs模塊來(lái)讀取文件。首先需要導(dǎo)入這個(gè)模塊:
const fs = require('fs');
2. 讀取文件內(nèi)容
使用fs.方法來(lái)異步讀取文件內(nèi)容:
fs.readFile('path/to/file.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
console.log(data); // 文件內(nèi)容

});
3. 同步讀取文件
如果需要同步讀取文件,可以使用fs.方法:
try {
const data = fs.readFileSync('path/to/file.txt', 'utf8');
console.log(data); // 文件內(nèi)容
} catch (err) {
console.error('Error reading the file:', err);
}
四、使用Blob對(duì)象在瀏覽器中創(chuàng)建文件
1. 創(chuàng)建Blob對(duì)象
在瀏覽器中,也可以使用Blob對(duì)象創(chuàng)建一個(gè)文件,并通過(guò)URL.生成一個(gè)下載鏈接:
const data = new Blob(['Hello, world!'], { type: 'text/plain' });
const url = URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = 'hello.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
2. 銷(xiāo)毀URL對(duì)象
使用完URL對(duì)象后,應(yīng)該調(diào)用URL.方法來(lái)釋放內(nèi)存:
URL.revokeObjectURL(url);
五、使用第三方庫(kù)處理文件
1. .js
.js是一個(gè)常用的庫(kù),用于在瀏覽器中保存文件。以下是一個(gè)示例:
import { saveAs } from 'file-saver';
const data = new Blob(['Hello, world!'], { type: 'text/plain' });
saveAs(data, 'hello.txt');
2. JSZip
JSZip是另一個(gè)常用的庫(kù),用于在瀏覽器中創(chuàng)建和解壓縮ZIP文件。以下是一個(gè)示例:
import JSZip from 'jszip';
import { saveAs } from 'file-saver';
const zip = new JSZip();
zip.file('hello.txt', 'Hello, world!');
zip.generateAsync({ type: 'blob' }).then(function(content) {
saveAs(content, 'example.zip');
});
六、文件操作的安全性與用戶(hù)體驗(yàn)
1. 文件權(quán)限
在瀏覽器環(huán)境中,出于安全考慮,只能訪問(wèn)用戶(hù)明確選擇的文件。無(wú)法直接訪問(wèn)用戶(hù)的文件系統(tǒng)。
2. 用戶(hù)體驗(yàn)
為了提升用戶(hù)體驗(yàn),可以使用文件選擇器進(jìn)行文件操作,并提供清晰的指引和錯(cuò)誤提示。例如:
document.getElementById('fileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
};
reader.onerror = function(e) {
console.error('Error reading the file:', e);
alert('Failed to read the file. Please try again.');
};
reader.readAsText(file);
} else {
alert('No file selected. Please choose a file.');
}
});
七、項(xiàng)目管理系統(tǒng)的集成
在研發(fā)項(xiàng)目管理中,文件操作經(jīng)常涉及到文檔、配置文件和數(shù)據(jù)文件的管理。推薦使用以下兩個(gè)系統(tǒng)來(lái)實(shí)現(xiàn)更加高效的項(xiàng)目管理:
1. 研發(fā)項(xiàng)目管理系統(tǒng)
是一款專(zhuān)業(yè)的研發(fā)項(xiàng)目管理系統(tǒng),提供了全面的文件管理功能。通過(guò)集成,可以實(shí)現(xiàn)文檔的集中管理和版本控制,提高團(tuán)隊(duì)協(xié)作效率。
2. 通用項(xiàng)目協(xié)作軟件
是一款通用項(xiàng)目協(xié)作軟件,支持文件共享和管理。通過(guò),可以輕松實(shí)現(xiàn)團(tuán)隊(duì)成員之間的文件共享和協(xié)作,提升工作效率。
總結(jié)
在中打開(kāi)文件可以通過(guò)多種方法實(shí)現(xiàn),包括使用File API讀取本地文件、通過(guò)fetch API獲取遠(yuǎn)程文件、以及通過(guò)Node.js的fs模塊處理服務(wù)器端文件。每種方法都有其適用的場(chǎng)景和優(yōu)勢(shì)。在實(shí)際應(yīng)用中,可以根據(jù)具體需求選擇合適的方法,并結(jié)合項(xiàng)目管理系統(tǒng)和,提高文件操作的效率和安全性。
相關(guān)問(wèn)答FAQs:
1. 如何在中打開(kāi)文件?
在中,要打開(kāi)文件,可以使用對(duì)象。以下是一個(gè)簡(jiǎn)單的示例代碼:
// 創(chuàng)建一個(gè)FileReader對(duì)象
var reader = new FileReader();
// 通過(guò)input元素選擇文件
var fileInput = document.getElementById('fileInput');
var file = fileInput.files[0];
// 讀取文件內(nèi)容
reader.readAsText(file);
// 當(dāng)文件讀取完成時(shí)觸發(fā)的事件
reader.onload = function(e) {
var contents = e.target.result;
console.log('文件內(nèi)容:', contents);
};
2. 如何在中通過(guò)文件路徑打開(kāi)文件?
在中,可以使用對(duì)象來(lái)通過(guò)文件路徑打開(kāi)文件。以下是一個(gè)示例代碼:
// 創(chuàng)建一個(gè)XMLHttpRequest對(duì)象
var xhr = new XMLHttpRequest();
// 打開(kāi)文件
xhr.open('GET', 'path/to/file.txt', true);
// 發(fā)送請(qǐng)求
xhr.send();
// 當(dāng)請(qǐng)求完成時(shí)觸發(fā)的事件
xhr.onload = function() {
if (xhr.status === 200) {
var contents = xhr.responseText;
console.log('文件內(nèi)容:', contents);
}
};
3. 如何在中通過(guò)URL打開(kāi)文件?
在中,可以使用fetch函數(shù)來(lái)通過(guò)URL打開(kāi)文件。以下是一個(gè)示例代碼:
// 通過(guò)URL打開(kāi)文件
fetch('https://example.com/path/to/file.txt')
.then(function(response) {
return response.text();
})
.then(function(contents) {
console.log('文件內(nèi)容:', contents);
})
.catch(function(error) {
console.log('發(fā)生錯(cuò)誤:', error);
});
請(qǐng)注意,這些示例代碼僅供參考,具體實(shí)現(xiàn)方式可能因使用的框架或庫(kù)而有所不同。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。