HTML文件到CSV Dataframe [重复]

bjg7j2ky  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(77)

此问题已在此处有答案

Using Python and BeautifulSoup (saved webpage source codes into a local file)(3个答案)
2小时前关闭
我试图打开并转换我的HTML文件到CSV,这样我就可以使用它作为一个dataframe。

import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'file:///C:/Users/jessi/OneDrive/Documents/posts.html'
response = request.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

print(soup)

得到这个错误:InvalidSchema:找不到“file://C://Users//jessi//OneDrive//Documents//posts.html”的连接适配器

i7uaboj4

i7uaboj41#

这里没有服务器可供请求。你有一个简单的文件。只要读它。

from bs4 import BeautifulSoup
import pandas as pd
filename = 'C:/Users/jessi/OneDrive/Documents/posts.html'
soup = BeautifulSoup(open(filename).read(), 'html.parser')
print(soup)

相关问题