Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个漂亮的脚本可以用正则表达式简化吗?_Python_Regex_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Python 这个漂亮的脚本可以用正则表达式简化吗?

Python 这个漂亮的脚本可以用正则表达式简化吗?,python,regex,web-scraping,beautifulsoup,python-requests,Python,Regex,Web Scraping,Beautifulsoup,Python Requests,我写了一些beautifulsoup脚本,其中有一部分看起来很多余,我在想是否可以用正则表达式简化它 来自这个论坛的所有帖子都用不同的颜色标记,我所做的就是用一行搜索每种颜色。对于六种颜色,我画了六行,只有一个单词不同 red = soup.find_all('a', style="font-weight: bold;color: red") blue = soup.find_all('a', style="font-weight: bold;color: blue

我写了一些beautifulsoup脚本,其中有一部分看起来很多余,我在想是否可以用正则表达式简化它

来自这个论坛的所有帖子都用不同的颜色标记,我所做的就是用一行搜索每种颜色。对于六种颜色,我画了六行,只有一个单词不同

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))
如果不是regex,或者是别的什么

这是部分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>

 
[] 
6.
2019-4-22
2/12234
5.02G/MP4
通过
                                
 
[] 
5.
2019-4-22
0/7265
3.85G/MP4
通过
                                
 
[] 
4.
2019-4-22
0/6226
23.39G/MP4
通过
                                

您可以将属性列表传递给css select with ends with operator

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


哪个论坛?有url吗?这只是中国的一些随机网站,呵呵~@qharr我们可以用url来帮助回答吗?当然可以,让我去看看@嘿,头儿,我查过了,有一些不合适的内容。呸~可怜
items = [item for item in soup.select("[style$='color: red'],[style$='color: green'],[style$='color: blue'],[style$='color: purple'],[style$='color: orange']")