使用pandas来抓取HTML:它可以用来抓取网页中的表吗?

2024-04-19 01:05:45 发布

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

我用BeautifulSoup和urllib做了一些基本的web垃圾处理。不过,我最近遇到了this链接,它说,要刮取像this这样的网页,只需运行:

import pandas as pd
tables = pd.read_html("https://apps.sandiego.gov/sdfiredispatch/")
print(tables[0])

我认为这太好了,不可能是真的,因为很多时候我都在与beautifulsoup和urllib2斗争。你知道吗

我试着把this页上的桌子拉出来:

url = "http://crdd.osdd.net/raghava/ahtpdb/display.php?details=1001"
tables = pd.read_html(url)
print tables[0]

我得到的结果是:

                              0
0  Detailed description of 1001 ID

我还尝试了其他一些方法,例如:

url = "http://crdd.osdd.net/raghava/ahtpdb/display.php?details=1001"
response = requests.get(url)
print response.content

或者类似于:

web_page = 'http://crdd.osdd.net/raghava/ahtpdb/display.php?details=1001'
page = urllib2.urlopen(web_page)
soup = BeautifulSoup(page, 'html.parser')
print soup.get_text()

我知道这里有很多使用各种方法的网页抓取的例子。正如你所看到的,我一直在遵循这些例子,只是我似乎无法让这个方法具体地解决我的问题。如果有人能告诉我他们将如何改进这些代码片段,以满足我的需要,我将不胜感激。你知道吗

编辑1:作为旁注,我在另一个网页上尝试了相同的代码:https://dbaasp.org/peptide-card?id=3,但我猜这个更复杂。你知道吗

编辑二:根据拉菲的建议,有些不寻常的地方。我已经附上了一个网页和网址,我试图刮here;拉菲你可以看到我的网址是略有不同,你用什么。当我试图在我的网址上运行你的建议时:

url = "http://crdd.osdd.net/raghava/ahtpdb/srcbr.php?details=1001"
table = pd.read_html(url)
print table[0]
print table[1]
print table[2]
print table[3]
print table[4]
print table[5]

我得到的输出如下(截断):

 Browse SOURCE in AHTPDB This page gives statis...
1                            Browse SOURCE in AHTPDB
2  This page gives statistics of SOURCE fields an...
3  Following table enlists the number of entries ...
4  Following table enlists the number of entries ...
5                                               Milk
6                                                834
7  google.load("visualization", "1", {packages:["...

                                                  1   \
0                            Browse SOURCE in AHTPDB
1                                                NaN
2                                                NaN
3  Following table enlists the number of entries ...
4                                                NaN
5                                             Casein
6                                                723
7                                                NaN

                                                  2   \
0  This page gives statistics of SOURCE fields an...
1                                                NaN
2                                                NaN
3                                               Milk
4                                                NaN
5                                             Bovine
6                                                477
7                                                NaN

                                                  3   \
0  Following table enlists the number of entries ...
1                                                NaN
2                                                NaN
3                                             Casein
4                                                NaN
5                                            Cereals
6                                                419
7                                                NaN

                                                  4        5       6   \
0  Following table enlists the number of entries ...     Milk  Casein
1                                                NaN      NaN     NaN
2                                                NaN      NaN     NaN
3                                             Bovine  Cereals    Fish
4                                                NaN      NaN     NaN
5                                               Fish     Pork   Human
6                                                384      333     215
7                                                NaN      NaN     NaN

        7        8        9   \
0   Bovine  Cereals     Fish
1      NaN      NaN      NaN
2      NaN      NaN      NaN
3     Pork    Human  Chicken
4      NaN      NaN      NaN
5  Chicken  Soybean      Egg
6      177      159       97
7      NaN      NaN      NaN

                         ...                             16     17     18  \
0                        ...                          723.0  477.0  419.0
1                        ...                            NaN    NaN    NaN
2                        ...                            NaN    NaN    NaN
3                        ...                          384.0  333.0  215.0
4                        ...                            NaN    NaN    NaN
5                        ...                            NaN    NaN    NaN
6                        ...                            NaN    NaN    NaN
7                        ...                            NaN    NaN    NaN

      19     20     21     22     23    24  \
0  384.0  333.0  215.0  177.0  159.0  97.0
1    NaN    NaN    NaN    NaN    NaN   NaN
2    NaN    NaN    NaN    NaN    NaN   NaN
3  177.0  159.0   97.0    NaN    NaN   NaN
4    NaN    NaN    NaN    NaN    NaN   NaN
5    NaN    NaN    NaN    NaN    NaN   NaN
6    NaN    NaN    NaN    NaN    NaN   NaN
7    NaN    NaN    NaN    NaN    NaN   NaN

                                                  25
0  google.load("visualization", "1", {packages:["...
1                                                NaN
2                                                NaN
3                                                NaN
4                                                NaN
5                                                NaN
6                                                NaN
7                                                NaN

[8 rows x 26 columns]
                         0
0  Browse SOURCE in AHTPDB
                                                   0
0  This page gives statistics of SOURCE fields an...
                                                  0   \
0  Following table enlists the number of entries ...
1  Following table enlists the number of entries ...
2                                               Milk
3                                                834
4  google.load("visualization", "1", {packages:["...

                                                  1       2        3       4   \
0  Following table enlists the number of entries ...    Milk   Casein  Bovine
1                                                NaN     NaN      NaN     NaN
2                                             Casein  Bovine  Cereals    Fish
3                                                723     477      419     384
4                                                NaN     NaN      NaN     NaN

        5      6        7        8        9   ...      12     13     14  \
0  Cereals   Fish     Pork    Human  Chicken  ...   834.0  723.0  477.0
1      NaN    NaN      NaN      NaN      NaN  ...     NaN    NaN    NaN
2     Pork  Human  Chicken  Soybean      Egg  ...     NaN    NaN    NaN
3      333    215      177      159       97  ...     NaN    NaN    NaN
4      NaN    NaN      NaN      NaN      NaN  ...     NaN    NaN    NaN

      15     16     17     18     19     20    21
0  419.0  384.0  333.0  215.0  177.0  159.0  97.0
1    NaN    NaN    NaN    NaN    NaN    NaN   NaN
2    NaN    NaN    NaN    NaN    NaN    NaN   NaN
3    NaN    NaN    NaN    NaN    NaN    NaN   NaN

我不明白这和我展示的截图有什么相似之处?是不是因为'details=1001'阻止了这个方法,因为它不像.php页面那样编写?你知道吗

编辑3:这样做:

url = 'http://crdd.osdd.net/raghava/ahtpdb/display.php?details=1001'
html = urllib.urlopen(url).read()
bs = BeautifulSoup(html, 'lxml')
tab = bs.find("table",{"class":"tab"})
data = []
rows = bs.find_all('tr')
for row in rows:
    cols = row.find_all('td')
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele])

print data

Tags: oftheinurlnumbersourcehtmlpage
1条回答
网友
1楼 · 发布于 2024-04-19 01:05:45

你应该在桌子的位置上玩一玩。 例如:我以您提供的网站为例,在那里找到一个表(url)。 然后我试了你试过的那段代码,但有一个小改动:

url = "http://crdd.osdd.net/raghava/ahtpdb/srcbr.php"
tables = pd.read_html(url)
print tables[4]

我得到的表刚刚好(与标题-没有问题,以删除它以后)。你知道吗

原因是,在您复制的示例代码中,只有一个表(或者多个表,并且他们需要的表是第一个表)。这就是为什么table[0]给了他们想要的桌子。在我在这里展示的例子中,网站使用表进行布局,第一个表不是您想要得到的表(在这种情况下是第五个表-这就是为什么table[4]在这种情况下可以工作的原因)

注意:您可能希望将其保存到csv,以便更易于阅读:

url = "http://crdd.osdd.net/raghava/ahtpdb/srcbr.php"
tables = pd.read_html(url)
tables[4].to_csv("path/to/file.csv")

根据您的信息,请尝试以下操作:

from bs4 import BeautifulSoup
import urllib.request

url = 'http://crdd.osdd.net/raghava/ahtpdb/display.php?details=1001'
html = urllib.request.urlopen(url).read()
bs = BeautifulSoup(html)
tab = bs.find("table",{"class":"tab"})
print(tab)

您将需要清理它,但是表的所有数据都应该在那里可用。你知道吗

相关问题 更多 >