如何用Python选择网页的具体表格

2024-04-18 20:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程和python的新手。但是我想在python脚本中解析HTML。在

以下是网页: http://stock.finance.sina.com.cn/hkstock/finance/00759.html

问题1

本页是关于特定股份的财务信息。这四张表是关于:

  1. 财务摘要
  2. 资产负债表
  3. 现金流
  4. 损益表。在

我想提取表3&4中的信息。这是我的代码:

import urllib
from bs4 import BeautifulSoup

url = 'http://stock.finance.sina.com.cn/hkstock/finance/00759.html'

html = urllib.urlopen(url).read()   #.read() mean read all into a string
soup = BeautifulSoup(html, "lxml")

table = soup.find("table", { "class" : "tab05" })
for row in table.findAll("tr"):
    print row.findAll("td")

但是这个代码只能得到第一个表的信息。如何更改代码以获取第三和第四个表信息?我发现这4个表不包含唯一的id或类名,我不知道如何定位它们。。。。在

问题2:

这也是简体中文网页,如何保持原文输出?在

问题3:

在每个表格的右上角有一个下拉菜单,用于选择适当的期间,即:“全部”“全年”“半年”“第一季度”“第三季度”urllib是否可以更改此下拉菜单?在

非常感谢。在


Tags: 代码com信息http网页readhtmlstock
2条回答

谢谢你的答复。 我可能误解了你的意思。我将代码重写如下:

tables = soup.findAll("table", { "class" : "tab05" })

print len(tables)

for row in tables[0].findAll("tr"):
    for col in row.findAll("td"):
        print col.getText()

“len(tables)”的结果是1。只能访问第一个表。 我还发现如果我使用

^{pr2}$

我无法得到那张桌子的全部信息。从这个代码得到的最后一个数字是“-45.7852”,这只是该表的一半。在

该网站称,这四个表的类名都是“tab05”。在

因此,您只需在var soup处将.find方法更改为.findAll,然后就可以访问所有四个表。在

import urllib
from bs4 import BeautifulSoup

url = 'http://stock.finance.sina.com.cn/hkstock/finance/00759.html'
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "lxml")

tables = soup.findAll("table", { "class" : "tab05" })
print len(tables) #4

for table in tables:
    for row in table.findAll("tr"):
        for col in row.findAll("td"):
            print col.getText()

对于简体中文的编码,print col.getText()将在终端上得到正确的单词。如果要将它们写入文件,则必须将字符串编码为gb2312。在

^{pr2}$

对于第三个问题,因为数据是用javascript函数来呈现的数据表.js,我认为不可能简单地通过urllib来获得所有这些。最好去别的图书馆看看,找到合适的用法。在

相关问题 更多 >