【问题标题】:Parsing html URL into pandas table将 html URL 解析为 pandas 表
【发布时间】:2020-10-02 13:48:19
【问题描述】:

我有以下网址link
有没有一种简单的方法可以直接从 URL 在 Jupyter 笔记本中创建熊猫表?
其中第一列对应单词(例如 Eachwhere),第二列对应括号内的内容(例如 adv),第三列对应括号后面的内容(例如 Everywhere)?

来自链接:

E () The fifth letter of the English alphabet.
E () E is the third tone of the model diatonic scale. E/ (E flat) is a tone which is intermediate between D and E.
E- () A Latin prefix meaning out, out of, from; also, without. See Ex-.
Each (a. / a. pron.) Every one of the two or more individuals composing a number of objects, considered separately from the rest. It is used either with or without a following noun; as, each of you or each one of you.
Each (a. / a. pron.) Every; -- sometimes used interchangeably with every.
Eachwhere (adv.) Everywhere.
Eadish (n.) See Eddish.

【问题讨论】:

  • 没有直接的方法可以实现您想要的。但是您可以使用 BeautifulSoup 来抓取内容,然后创建数据框。

标签: python pandas parsing nlp jupyter-notebook


【解决方案1】:

如 cmets 中所述,您可以使用诸如 beautifulsoup 或 lxml 之类的库来完成工作。有几种方法可以接近它。这里有一个,使用 beautifulsoup,例如:

import pandas as pd
from bs4 import BeautifulSoup as bs
import requests

url = "http://www.mso.anu.edu.au/%7Eralph/OPTED/v003/wb1913_e.html"
req=requests.get(url)

soup = bs(req.text,'lxml')
columns = ['word','part','meaning']
entries = []
for p in soup.select('p'):
    entry = []
    prt = p.select_one('i').text if len(p.select_one('i').text)>0 else "na"
    entry.extend([p.select_one('b').text, prt, p.text.split(') ')[-1]])
    entries.append(entry)
pd.DataFrame(entries, columns=columns)

输出:

   word part    meaning
0   E   na  The fifth letter of the English alphabet.
1   E   na  is a tone which is intermediate between D and E.
2   E-  na  A Latin prefix meaning out, out of, from; also...
3   Each    a. / a. pron.   Every one of the two or more individuals compo...

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 2011-09-13
    • 2016-09-03
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多