Python 使用导入时间加快beautiful soup(删除太多无关数据)

Python 使用导入时间加快beautiful soup(删除太多无关数据),python,time,beautifulsoup,Python,Time,Beautifulsoup,我的程序将正常运行并打印所需的输出,但运行需要一分钟。它获取整个页面的数据,然后在数据中搜索所需信息。延迟在于抓取数据。基本上,我正在寻找的是一种只获取包含所需信息的页面的数据的方法,而不是从整个页面中获取所有不相关的数据,从而大大降低过程的速度 我需要的信息是包含“$”和“LDK2-ENY10”的第一个数据块,变量z仅用于获取第一个数据块,而不是之后的所有无关数据 import requests from time import time from bs4 import BeautifulSo

我的程序将正常运行并打印所需的输出,但运行需要一分钟。它获取整个页面的数据,然后在数据中搜索所需信息。延迟在于抓取数据。基本上,我正在寻找的是一种只获取包含所需信息的页面的数据的方法,而不是从整个页面中获取所有不相关的数据,从而大大降低过程的速度

我需要的信息是包含“$”和“LDK2-ENY10”的第一个数据块,变量z仅用于获取第一个数据块,而不是之后的所有无关数据

import requests
from time import time
from bs4 import BeautifulSoup     
z = 0;
link = "http://yugiohprices.com/get_card_prices/Dark+Magician?_={}"
r = requests.get(link.format(int(time())))
soup = BeautifulSoup(r.content, "lxml")
rawdata = soup.find_all("td")
for thing in rawdata:    
  if "LDK2-ENY10" in str(thing) and "$" in str(thing) and z == 0:
    print thing; z = 1;
这是当前的输出,可能只需要删除这个输出就足够快了,它似乎有点多余,因为我只需要第10行(
$0.33
)、第18行(
$0.77
)和第29行(
$5.28
)中的信息,但是在这一点上我不在乎,只希望程序运行不需要2-5分钟。。(30秒或更短的时间将是惊人的)

请让我知道,如果有什么东西从我的职位失踪,我会更新它

<td style="width: 206px; padding-right: 10px" valign="top">
<table border="1" id="item_stats" style="margin-bottom: 10px">
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
              Lowest
              <a alt="How are lowest prices calculated?" href="http://blog.yugiohprices.com/post/90183367316/lowest-card-price-is-now-picked-using-ebay-listings" target="_blank" title="How are lowest prices calculated?">(?)</a>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 2px">
<b>$0.33</b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">Highest</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 2px">
                $5.28
              </p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
              Average
              <a alt="How are average prices calculated?" href="http://blog.yugiohprices.com/post/54460976914/how-are-average-prices-calculated" target="_blank" title="How are average prices calculated?">(?)</a>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 2px">
                $0.77
              </p>
</td>
</tr>
</table>
<table border="1" id="item_stats">
<tr style="height: 22px">
<td class="key" style="border: 1px solid #000; width: 80px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>
                  Shift
                </b>
<br/>
                (24 Hours)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b style="color: red">
                  -9.41%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 80px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (3 Days)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b style="color: red">
                  -2.53%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 80px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (1 Week)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b style="color: red">
                  -9.41%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (3 Weeks)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b>
                  0%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (30 Days)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b>
                  0%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (3 Months)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b>
                  0%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (6 Months)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b>
                  0%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" style="border: 1px solid #000; width: 94px">
<p style="margin-top: -6px; margin-bottom: -4px; margin-left: 0px; margin-right: -6px; font-weight: normal">
<b>Shift</b>
<br/>
                (1 Year)
              </p>
</td>
<td style="border: 1px solid #000; text-align: center">
<p style="margin: 6px; margin-bottom: 9px">
<b>
                  0%
                </b>
</p>
</td>
</tr>
<tr>
<td class="key" colspan="2" style="text-align: center; border: 1px solid #000">
<a href="/price_history/LDK2-ENY10?rarity=Common" target="_blank">View History</a>
</td>
</tr>
</table>
<br/>
<div align="center">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Yugioh Prices Skyscraper -->
<ins class="adsbygoogle" data-ad-client="ca-pub-7333610178228936" data-ad-slot="9136249004" style="display:inline-block;width:160px;height:600px"></ins>
<script>
            (adsbygoogle = window.adsbygoogle || []).push({});
          </script>
</div>
</td>

最低的

$0.33

最高

$5.28

平均值

$0.77

移位
(24小时)

-9.41%

移位
(3天)

-2.53%

移位
(1周)

-9.41%

移位
(3周)

0%

移位
(30天)

0%

移位
(3个月)

0%

移位
(6个月)

0%

移位
(1年)

0%


(adsbygoogle=window.adsbygoogle | |[]).push({});
首先,解析是瓶颈的几率非常低-请重新检查该假设


第一个合乎逻辑的事情是去掉
z
变量,并在找到所需信息后简单地中断循环。这将对执行时间产生重大影响:

for thing in rawdata:    
    thing_html = str(thing)  # avoiding calling str() two times per iteration
    if "LDK2-ENY10" in thing_html and "$" in thing_html:
        print(thing)
        break

或者/和,您可以避免解析完整的页面和源代码。大致如下:

from bs4 import BeautifulSoup, SoupStrainer

td_only = SoupStrainer("td")
soup = BeautifulSoup(r.content, "lxml", parse_only=td_only)
不过,考虑到树的相对较小,我怀疑“仅解析”方法是否会产生重大影响



您还可以尝试使用解释器运行脚本,而不是使用常规的
CPython
。您需要从
lxml
切换到
html.parser
html5lib
(或者您可以),但是,经过几次快速测试后,我发现它的性能比
CPython
+
lxml
好,非常感谢!你不知道这是多么令人沮丧><我真的很感激!谢谢,我想我已经把它标记为解决了。最后一个问题,您是否知道是否有一种方法可以只在标签中加载标签?还是只加载前几个标记的方法?我很抱歉对这件事这么陌生