# 正则求助，嵌套的div

**URL:** <https://forum.freemdict.com/t/topic/15558>\
**Category:** 技术交流与词典编修\
**Created:** [2022 年9 月 22 日 02:32 UTC](https://forum.freemdict.com/t/topic/15558 "2022-09-22T02:32:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 02:32 UTC](https://forum.freemdict.com/t/topic/15558/1 "2022-09-22T02:32:39Z")

</div>

好像之前有人问过，没找到。  
`......<div class="thesaurus" type="tabt"><div class="beigeBox">......</div><div class="entry">......</div></div>......`  
如何获取class="thesaurus"的div里面的所有内容，多谢

---

<div class="post-metadata">

作者： ![random](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/random/32/19006_2.png) [@random](https://forum.freemdict.com/u/random)\
发布日期： [2022 年9 月 22 日 03:01 UTC](https://forum.freemdict.com/t/topic/15558/2 "2022-09-22T03:01:39Z")

</div>

正则很难实现，建议用python+bs

---

<div class="post-metadata">

作者： ![mdict6](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@mdict6](https://forum.freemdict.com/u/mdict6)\
发布日期： [2022 年9 月 22 日 03:05 UTC](https://forum.freemdict.com/t/topic/15558/3 "2022-09-22T03:05:08Z")

</div>

送伊一颗小❤

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 03:07 UTC](https://forum.freemdict.com/t/topic/15558/4 "2022-09-22T03:07:15Z")

</div>

原来如此，我在网上找半天没找到

---

<div class="post-metadata">

作者： ![ylxdxx](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/y/b2d939/32.png) [@ylxdxx](https://forum.freemdict.com/u/ylxdxx)\
发布日期： [2022 年9 月 22 日 05:45 UTC](https://forum.freemdict.com/t/topic/15558/6 "2022-09-22T05:45:49Z")

</div>

找一个xml解析的工具或相关的库，很好处理的

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 06:25 UTC](https://forum.freemdict.com/t/topic/15558/7 "2022-09-22T06:25:46Z")

</div>

可能是因为我的文件没写全，你给的正则不行，我传上来可以麻烦再看看吗

[code.txt](https://forum.freemdict.com/uploads/short-url/6RoWcqMyCiEKc6HrYVF2UAnEeGj.txt) (18.1 KB)

---

<div class="post-metadata">

作者： ![舒服员](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/%E8%88%92/13edae/32.png) [@舒服员](https://forum.freemdict.com/u/%E8%88%92%E6%9C%8D%E5%91%98)\
发布日期： [2022 年9 月 22 日 17:30 UTC](https://forum.freemdict.com/t/topic/15558/8 "2022-09-22T17:30:28Z")

</div>

```auto

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [noun] > zygote or syngamete (12)
              
zygote1891
A body of living protoplasm, as a cell or cell-nucleus, formed by the conjugation or fusion of two
                  such bodies in reproduction; a zygospore, or any…

zygotoid1891
a multinucleate form of zygote in certain fungi (see quot.).

syngametea1900
The cell produced by the fusion of two gametes in reproduction.

ookinete1902
A zygote capable of autonomous movement, esp. as a stage in the life cycle of some parasitic
                  protozoa.

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [adjective] > zygote or syngamete
                (15) 
syngamic1904

syngamous1904

zygotic1909
pertaining to or of the nature of a zygote, produced or characterized by zygosis.

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [adverb] > zygote (1) 
zygotically1915
in the zygote; in terms of the zygote.

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [noun] > zygote or syngamete >
                zygotomere, etc. (3) 
zoozygosphere1880
(in algae and fungi) a motile gamete; also called planogamete, zoogamete.

zygotoblast1899
one of a number of germ-cells or sporozoites produced by budding from a zygotomere (see
                  below).

zygotomere1899
one of a number of cells formed by segmentation of a zygote in the malaria parasite or other
                  Sporozoa.

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [noun] > zygote or syngamete (12)
              
zygote1891
A body of living protoplasm, as a cell or cell-nucleus, formed by the conjugation or fusion of two
                  such bodies in reproduction; a zygospore, or any…

zygotoid1891
a multinucleate form of zygote in certain fungi (see quot.).

syngametea1900
The cell produced by the fusion of two gametes in reproduction.

ookinete1902
A zygote capable of autonomous movement, esp. as a stage in the life cycle of some parasitic
                  protozoa.

the world > life > biology >
                biological processes > procreation or reproduction > reproductive
                  substances or cells > [noun] > zygote or syngamete >
                zygotomere, etc. (3) 
zoozygosphere1880
(in algae and fungi) a motile gamete; also called planogamete, zoogamete.

zygotoblast1899
one of a number of germ-cells or sporozoites produced by budding from a zygotomere (see
                  below).

zygotomere1899
one of a number of cells formed by segmentation of a zygote in the malaria parasite or other
                  Sporozoa.

```

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 18:53 UTC](https://forum.freemdict.com/t/topic/15558/10 "2022-09-22T18:53:13Z")

</div>

是这种结果，正则没看到 😀

---

<div class="post-metadata">

作者： ![Moguer](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/moguer/32/20527_2.png) [@Moguer](https://forum.freemdict.com/u/Moguer)\
发布日期： [2022 年9 月 22 日 19:57 UTC](https://forum.freemdict.com/t/topic/15558/11 "2022-09-22T19:57:47Z")

</div>

可以直接用XPath找。

 ![image](https://forumcdn.freemdict.com/uploads/default/original/3X/1/8/180c850cc4079480b9603281102611f2b7855e53.png)

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 22:39 UTC](https://forum.freemdict.com/t/topic/15558/12 "2022-09-22T22:39:28Z")

</div>

效率如何，差不多7g，我用python+bs花了好几个小时（代码很简陋）

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 22 日 22:40 UTC](https://forum.freemdict.com/t/topic/15558/13 "2022-09-22T22:40:28Z")

</div>

这个是正则写出来的吗

---

<div class="post-metadata">

作者： ![Moguer](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/moguer/32/20527_2.png) [@Moguer](https://forum.freemdict.com/u/Moguer)\
发布日期： [2022 年9 月 23 日 02:34 UTC](https://forum.freemdict.com/t/topic/15558/14 "2022-09-23T02:34:42Z")

</div>

不知……我对这个一窍不通的。 🥲

---

<div class="post-metadata">

作者： ![舒服员](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/%E8%88%92/13edae/32.png) [@舒服员](https://forum.freemdict.com/u/%E8%88%92%E6%9C%8D%E5%91%98)\
发布日期： [2022 年9 月 23 日 11:13 UTC](https://forum.freemdict.com/t/topic/15558/15 "2022-09-23T11:13:32Z")

</div>

我用bs搞的, 你用的什么解析器? 试试lxml呢.

正则的话, 我只能分两步搞, 先匹配到thesaurus标签, 再提取text

```auto
thesaurus_tag = re.compile(r'<div\s+class=\"thesaurus\".*?>\s+<div\s+class=\"entry\">.*?</div>\s+</div>', re.DOTALL)
unwanted = re.compile(r"<(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\">])+>", re.DOTALL)

```

或者你试试先匹配出thesaurus标签, 再用bs提取文字, thesaurus标签其实没有几个

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 23 日 11:26 UTC](https://forum.freemdict.com/t/topic/15558/16 "2022-09-23T11:26:14Z")

</div>

我就随手用的html.parser，效率会差很多吗，大意了啊，7个g花了好几个小时 🥲下次再试试你的方法

---

<div class="post-metadata">

作者： ![hua](https://forumcdn.freemdict.com/user_avatar/forum.freemdict.com/hua/32/11090_2.png) [@hua](https://forum.freemdict.com/u/hua)\
发布日期： [2022 年9 月 23 日 13:11 UTC](https://forum.freemdict.com/t/topic/15558/17 "2022-09-23T13:11:50Z")

</div>

会差很多。

`pip install lxml`

然后代码里面：

`soup = BeautifulSoup(html, 'lxml')`

---

<div class="post-metadata">

作者： ![zheshijie](https://forumcdn.freemdict.com/letter_avatar_proxy/v4/letter/z/73ab20/32.png) [@zheshijie](https://forum.freemdict.com/u/zheshijie)\
发布日期： [2022 年9 月 23 日 16:55 UTC](https://forum.freemdict.com/t/topic/15558/18 "2022-09-23T16:55:51Z")

</div>

学到了，是我自作聪明了，看网上别人也都是用lxml 😅
