python - 如何从没有标识符的 <td> 属性中仅提取网络中的数字(by beautiful soup)

标签 python search web-scraping beautifulsoup int

得到对我的http请求的响应后,我需要识别响应中的ID并为其分配值。 基本上我做的程序加载页面,通过ID搜索产品,然后读取网页内容并为该产品分配实际库存值(value)。

test = s.get(test)
soup = BeautifulSoup(test.text, "html.parser")
table = soup.find("table",class_="table")
code = table.find("small",class_="code")

listTd=table.findAll("td") #iam able to find all td where are data saved.
#Here i don´t know how to identify the corrrect td which contains only int values ( it pressents actual number of products on stock, i need extract that).#


#Here is response from web:
[<td>
<a href="/Products/19321">
<small class="code">Code: 64SPW.100</small>
<span class="product_description">
<span class="description">Ø10x22x72 ØG.10</span>
</span>
</a>
</td>, <td>
                        72.37 EUR/NP
                        <br/>
<span class="iva">(excluding VAT 0%)</span>
</td>, <td>
                        55%
                    </td>, <td>
                        32.57 EUR/NP
                        <br/>
<span class="iva">(excluding VAT 0%)</span>
</td>, <td>
44                    </td>, <td class="shopping">
<div class="add_to_cart">
<form action="/Products/19321/AddToCart?returnUrl=%2FProducts%2FSearch%3Fquery%3D64SPW.100" method="post"> <div class="input-append">
<input autocomplete="off" class="span1" name="quantity" type="text" value="1"/>
<span class="add-on">NP</span>
</div>
<input class="btn btn-primary loader" type="submit" value="Add to cart"/>
</form>
</div>
</td>, <td>
<a href="/Products/19322">
<small class="code">Code: 64SPW.100C</small>
<span class="product_description">
<span class="description">Ø10x22x72 ØG.10 Cer-A</span>
</span>
</a>
</td>, <td>
                        79.44 EUR/NP
                        <br/>
<span class="iva">(excluding VAT 0%)</span>
</td>, <td>
                        55%
                    </td>, <td>
                        35.75 EUR/NP
                        <br/>
<span class="iva">(excluding VAT 0%)</span>
</td>, <td>
8                    </td>,

正如您在代码中看到的:代码:64SPW.100,这是因为我采用 64SPW.100 值并在网络上搜索它,并且必须以某种方式将其与值 44 链接起来。但是没有标识符可以根据该标识符我可以找到这个值并分配给搜索到的正确代码 到底有什么办法吗?

这里是否有任何可能的方式如何将代码:“值”与 44 <\td> 中的正确值链接起来?我无法编写标尺,它会在正确的代码后找到属性:64SPW.100,并识别出该属性中只有 int 数字并将其分配给代码?

因此,对于响应中的每个代码都应该清楚,如果存在序列 Code:64SP.100 将与最接近的序列 <.td> 链接,仅包含 int 数字 <\td>

最佳答案

如果我正确理解你的问题,你需要找到下一个 <td>标签中仅包含整数。

code.find_next('td', text=re.compile(r'^[\n\s]*\d+[\n\s]*$')

您可能需要针对您的具体情况调整正则表达式。我只匹配带有空格或换行符的正整数。

关于python - 如何从没有标识符的 <td> 属性中仅提取网络中的数字(by beautiful soup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356255/

相关文章:

python - 使用python在单个pdf页面上保存多个绘图

python - 使用 ruby​​ 或 python 在文件中查找

go - 如何使用 Golang 单击按钮进行 Web 抓取

javascript - 无法使用 Selenium webdriver、python 在 <span> 标记内模拟 onclick javascript

python - python2.6 中子样本 0.0.6 的错误

python - NLTK - 抛出 Stem() 缺少 1 个必需参数错误

python - Numpy,长数组的问题

search - 如何为一对多关系配置 Solr

c# - 在列表框中搜索

javascript - DOM 准备好后立即开始抓取 Nightmarejs