首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析HTML以从表行中提取数据

解析HTML以从表行中提取数据
EN

Stack Overflow用户
提问于 2018-06-27 02:28:24
回答 1查看 329关注 0票数 0

我正在使用BeautifulSoup从纳斯达克网站提取股票信息。我想特别从HTML页面上的表行中检索信息,但总是得到一个错误(第12行)。

代码语言:javascript
复制
    #import html-parser
    from bs4 import BeautifulSoup
    from requests import get

    url = 'https://www.nasdaq.com/symbol/amzn' #AMZN is just an example
    response = get(url)

    #Create parse tree (BeautifulSoup Object)
    soup = BeautifulSoup(response.text, 'html.parser')
    data = soup.find_all(class_= 'column span-1-of-2')

    table = data.find(class_= 'table-row') #This is where the error occurs
    print(table)
EN

回答 1

Stack Overflow用户

发布于 2018-06-28 21:09:56

您可以这样做,以便从表行中获取数据。

代码语言:javascript
复制
import requests
from bs4 import BeautifulSoup
import re

r = requests.get("https://www.nasdaq.com/")

print(r)
soup = BeautifulSoup(r.content, 'html.parser')
data = soup.find('table',{'id':'indexTable', 'class':'floatL marginB5px'}).script.text
matches = re.findall(r'nasdaqHomeIndexChart.storeIndexInfo(.*);\r\n', data)
table_rows = [re.findall(r'\".*\"', row) for row in matches]
print(table_rows)

table_rows是包含表数据的列表的列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51049378

复制
相关文章

相似问题

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