Можно ли упростить этот скрипт BeautifulSoup с помощью Regex? Или это еще? - PullRequest
0 голосов
/ 24 апреля 2019

Я написал несколько сценариев BeautifulSoup, и одна часть кажется излишней, я думаю, можно ли ее упростить с помощью Regex.

Все сообщения на этом форуме помечены разными цветами, и я выполнил поиск каждого цвета одной строкой. Для шести цветов я сделал шесть строк с одним отличием.

    red = soup.find_all('a', style="font-weight: bold;color: red")
    blue = soup.find_all('a', style="font-weight: bold;color: blue")
    green = soup.find_all('a', style="font-weight: bold;color: green")
    purple = soup.find_all('a', style="font-weight: bold;color: purple")
    orange = soup.find_all('a', style="font-weight: bold;color: orange")
    lime = soup.find_all('a', style="color: green")

Я не уверен, что это можно упростить. Может быть, что-то вроде

re.compile("(color: red|blue|green|purple|orange)", re.(whatever the letter is))

если это не регулярное выражение, или это может быть что-то еще? Спасибо, спасибо за помощь любителям ~

Это частичный DOM:

<th class="common">
<label>
<img alt="" src="images/green001/agree.gif"/>
<img alt="本版置顶" src="images/green001/pin_1.gif"/>
                                                             </label>
<em>[<a href="forumdisplay.php?fid=230&amp;filter=type&amp;typeid=140">美臀</a>]</em> <span id="thread_10431427"><a href="thread-10431427-1-1.html" style="font-weight: bold;color: blue">(本中)(HND-???) 二宮ひかり</a></span>
<img alt="附件" class="attach" src="images/attachicons/common.gif"/>
</th>
<td class="author">
<cite>
<a href="space.php?action=viewpro&amp;uid=12737809">第一會所新片</a><img align="absmiddle" border="0" src="images/thankyou.gif"/>6                                        </cite>
<em>2019-4-22</em>
</td>
<td class="nums"><strong>2</strong> / <em>12234</em></td>
<td class="nums">5.02G / MP4                
                </td>
<td class="lastpost">
<em><a href="redirect.php?tid=10431427&amp;goto=lastpost#lastpost">2019-4-23 20:22</a></em>
<cite>by <a href="space.php?action=viewpro&amp;username=zj376104288">zj376104288</a></cite>
</td>
</tr>
</tbody><!-- 三級置頂分開 -->
<!-- 三級置頂分開 -->
<tbody id="stickthread_10431424">
<tr>
<td class="folder"><a href="thread-10431424-1-1.html" target="_blank" title="新窗口打开"><img src="images/green001/folder_common.gif"/></a></td>
<td class="icon">
                                                                    </td>
<th class="common">
<label>
<img alt="" src="images/green001/agree.gif"/>
<img alt="本版置顶" src="images/green001/pin_1.gif"/>
                                                             </label>
<em>[<a href="forumdisplay.php?fid=230&amp;filter=type&amp;typeid=1303">VR</a>]</em> <span id="thread_10431424"><a href="thread-10431424-1-1.html" style="font-weight: bold;color: red">(WAAP)(WPVR-???)葵百合香</a></span>
<img alt="附件" class="attach" src="images/attachicons/common.gif"/>
</th>
<td class="author">
<cite>
<a href="space.php?action=viewpro&amp;uid=12737809">第一會所新片</a><img align="absmiddle" border="0" src="images/thankyou.gif"/>5                                        </cite>
<em>2019-4-22</em>
</td>
<td class="nums"><strong>0</strong> / <em>7265</em></td>
<td class="nums">3.85G / MP4                
                </td>
<td class="lastpost">
<em><a href="redirect.php?tid=10431424&amp;goto=lastpost#lastpost">2019-4-22 20:57</a></em>
<cite>by <a href="space.php?action=viewpro&amp;username=%B5%DA%D2%BB%95%FE%CB%F9%D0%C2%C6%AC">第一會所新片</a></cite>
</td>
</tr>
</tbody><!-- 三級置頂分開 -->
<!-- 三級置頂分開 -->
<tbody id="stickthread_10431423">
<tr>
<td class="folder"><a href="thread-10431423-1-1.html" target="_blank" title="新窗口打开"><img src="images/green001/folder_common.gif"/></a></td>
<td class="icon">
                                                                    </td>
<th class="common">
<label>
<img alt="" src="images/green001/agree.gif"/>
<img alt="本版置顶" src="images/green001/pin_1.gif"/>
                                                             </label>
<em>[<a href="forumdisplay.php?fid=230&amp;filter=type&amp;typeid=1303">VR</a>]</em> <span id="thread_10431423"><a href="thread-10431423-1-1.html" style="font-weight: bold;color: red">(KMP)(SAVR-???)舞島あかり</a></span>
<img alt="附件" class="attach" src="images/attachicons/common.gif"/>
</th>
<td class="author">
<cite>
<a href="space.php?action=viewpro&amp;uid=12737809">第一會所新片</a><img align="absmiddle" border="0" src="images/thankyou.gif"/>4                                        </cite>
<em>2019-4-22</em>
</td>
<td class="nums"><strong>0</strong> / <em>6226</em></td>
<td class="nums">23.39G / MP4               
                </td>
<td class="lastpost">
<em><a href="redirect.php?tid=10431423&amp;goto=lastpost#lastpost">2019-4-22 20:57</a></em>
<cite>by <a href="space.php?action=viewpro&amp;username=%B5%DA%D2%BB%95%FE%CB%F9%D0%C2%C6%AC">第一會所新片</a></cite>
</td>
</tr>
</tbody><!-- 三級置頂分開 -->
<!-- 三級置頂分開 -->
<tbody id="stickthread_10431422">
<tr>
<td class="folder"><a href="thread-10431422-1-1.html" target="_blank" title="新窗口打开"><img src="images/green001/folder_common.gif"/></a></td>
<td class="icon">
                                                                    </td>

1 Ответ

1 голос
/ 25 апреля 2019

Вы можете передать список атрибутов в css select, заканчивая оператором

[style$='color: red'],[style$='color: green'],[style$='color: blue'],[style$='color: purple'],[style$='color: orange']

Итак,

items = [item for item in soup.select("[style$='color: red'],[style$='color: green'],[style$='color: blue'],[style$='color: purple'],[style$='color: orange']")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...