首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python BeautifulSoup -从网页中解析表格时出现问题

Python BeautifulSoup -从网页中解析表格时出现问题
EN

Stack Overflow用户
提问于 2018-07-18 03:44:00
回答 1查看 157关注 0票数 0

我想解析来自以下站点的表数据:Pricing data,并创建一个包含所有表值(vCPU、内存、存储、价格)的数据帧。但是,使用下面的代码,我似乎找不到页面上的表。有人能帮我找出如何解析这些值吗?

使用pd.read_html时,会出现找不到表的错误。

代码语言:javascript
复制
import pandas as pd
from bs4 import BeautifulSoup
import requests
import csv


url = "https://aws.amazon.com/ec2/pricing/on-demand/"
r = requests.get(url)
html_content = r.text
soup = BeautifulSoup(html_content, 'html.parser')
data=[]
tables = soup.find_all('table')


df = pd.read_html(url)

EN

回答 1

Stack Overflow用户

发布于 2018-07-18 05:12:23

如果你因为动态内容而遇到麻烦,一个很好的变通方法是selenium,它模拟了浏览器体验,所以你不必担心管理cookie和其他动态web内容带来的问题。我可以用下面的代码抓取页面:

代码语言:javascript
复制
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get('https://aws.amazon.com/ec2/pricing/on-demand/')
sleep(3)
html = driver.page_source
soup = BeautifulSoup(html,'lxml')
driver.close()
data=[]
tables = soup.find_all('table')
print(tables)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51389107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档