I wrote some beautifulsoup scripts, and one part seems really redundant, I am thinking if it can be simplified with Regex.
All posts from this forum are marked with different colors, what I did is to search each color with one line. For six colors I did six lines with only one words difference.
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")
I am not sure if it is possible to be simplified. Maybe something like:
re.compile("(color: red|blue|green|purple|orange)", re.(whatever the letter is))
if it’s not regex, or could it be something else?
This is partial 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&filter=type&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&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&goto=lastpost#lastpost">2019-4-23 20:22</a></em> <cite>by <a href="space.php?action=viewpro&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&filter=type&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&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&goto=lastpost#lastpost">2019-4-22 20:57</a></em> <cite>by <a href="space.php?action=viewpro&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&filter=type&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&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&goto=lastpost#lastpost">2019-4-22 20:57</a></em> <cite>by <a href="space.php?action=viewpro&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>
Advertisement
Answer
You can pass a attribute list to css select with ends with operator
[style$='color: red'],[style$='color: green'],[style$='color: blue'],[style$='color: purple'],[style$='color: orange']
So,
items = [item for item in soup.select("[style$='color: red'],[style$='color: green'],[style$='color: blue'],[style$='color: purple'],[style$='color: orange']")