如何从属性数据内容的按钮提取数据或数字="&引用;在python中使用BeautifulSoup4,如下所示 显示电话号码。

如何从属性数据内容的按钮提取数据或数字="&引用;在python中使用BeautifulSoup4,如下所示 显示电话号码。,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,数据内容与任何其他属性一样-id,类,名称,href,src,所以您可以使用相同的方法从元素中获取它。之后,您必须使用标准字符串函数或正则表达式。最终,您可能需要将和替换为,您将拥有另一个HTML,您可以再次使用BeautifulSoup <button class="btn btn-success phone_number_btn pull-right" data-ad-id="4598214" data

数据内容
与任何其他属性一样-
id
名称
href
src
,所以您可以使用相同的方法从元素中获取它。之后,您必须使用标准字符串函数或正则表达式。最终,您可能需要将
替换为
,您将拥有另一个
HTML
,您可以再次使用
BeautifulSoup

 <button class="btn btn-success phone_number_btn pull-right" data-ad-id="4598214" 
                    data-content="
                 &lt;div class='text-center owner-info-popover'&gt;

                  &lt;div class='primary-lang'&gt;Danish&lt;/div&gt;
                   &lt;h4 style='display:inline-block' class='nomargin mt5 

generic-green primary-
lang'&gt;&lt;i ***class='fa fa-phone'&gt;&lt;/i&gt; 03333315122&lt;/h4&gt;***
                       &lt;p style='margin:10px -15px 0' class='fs12'&gt;Mention PakWheels.com when calling Seller to get a good deal&lt;/p&gt;
                     &lt;/div&gt;
                   " data-html="true" data-placement="bottom" data-price="4260000" onclick="trackEvents('UsedCars','ContactPhone','Search-Free');
                       adPhoneCount('/used-cars/4598214/increment_phone_count')" rel="popover"><i class="fa fa-phone"></i>Show Phone No.</button>

使用访问属性的标准方法,您可以相对轻松地从“数据内容”中提取整个值。一旦你有了这个值,你可能应该使用一个正则表达式来过滤你需要的信息?显示您的代码。并在问题的正文中描述问题,而不是在标题中。
数据内容
与任何其他属性一样-
id
名称
href
src
-因此您可以使用相同的方法从元素中获取
项[“数据内容”]
。之后,您必须使用标准字符串函数或正则表达式。最终,您可能会将
替换为
,您将拥有另一个HTML,可以与
BeautifulSoup
一起使用。为什么您再次解析HTML?请详细说明
BS
数据内容=“文本与HTML”
视为普通字符串,而不是HTML,它不解析它-所以我必须得到这个字符串,自己解析它。
html = '''<button class="btn btn-success phone_number_btn pull-right" data-ad-id="4598214" 
                    data-content="
                 &lt;div class='text-center owner-info-popover'&gt;

                  &lt;div class='primary-lang'&gt;Danish&lt;/div&gt;
                   &lt;h4 style='display:inline-block' class='nomargin mt5 

generic-green primary-
lang'&gt;&lt;i class='fa fa-phone'&gt;&lt;/i&gt; 03333315122&lt;/h4&gt;
                       &lt;p style='margin:10px -15px 0' class='fs12'&gt;Mention PakWheels.com when calling Seller to get a good deal&lt;/p&gt;
                     &lt;/div&gt;
                   " data-html="true" data-placement="bottom" data-price="4260000" onclick="trackEvents('UsedCars','ContactPhone','Search-Free');
                       adPhoneCount('/used-cars/4598214/increment_phone_count')" rel="popover"><i class="fa fa-phone"></i>Show Phone No.</button>'''


from bs4 import BeautifulSoup as BS

soup = BS(html, 'html.parser')

item = soup.find('button')
text = item['data-content']
#print(text)
 
soup = BS(text, 'html.parser')
item = soup.find('h4')
print(item.get_text(strip=True))
03333315122