溫馨提示×

怎么用python爬取文本內容并保存

小億
111
2023-11-07 13:38:34
欄目: 編程語言

要用Python爬取文本內容并保存,可以按照以下步驟進行:

  1. 導入所需的庫:首先,導入requests庫,用于發(fā)送HTTP請求獲取網(wǎng)頁內容;導入BeautifulSoup庫,用于解析網(wǎng)頁內容。
import requests
from bs4 import BeautifulSoup
  1. 發(fā)送HTTP請求并獲取網(wǎng)頁內容:使用requests庫的get方法發(fā)送GET請求,并通過text屬性獲取網(wǎng)頁內容。
url = '要爬取的網(wǎng)頁URL'
response = requests.get(url)
html = response.text
  1. 解析網(wǎng)頁內容:使用BeautifulSoup庫解析網(wǎng)頁內容,并提取所需的文本信息。
soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()
  1. 保存文本內容:將提取到的文本內容保存到文件中,可使用open函數(shù)打開一個文件,然后使用write方法寫入內容。
with open('保存的文件路徑', 'w', encoding='utf-8') as file:
    file.write(text)

完整代碼示例:

import requests
from bs4 import BeautifulSoup

url = '要爬取的網(wǎng)頁URL'
response = requests.get(url)
html = response.text

soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()

with open('保存的文件路徑', 'w', encoding='utf-8') as file:
    file.write(text)

請將代碼中的要爬取的網(wǎng)頁URL替換為你需要爬取的網(wǎng)頁的URL,保存的文件路徑替換為你希望保存的文件路徑。

0