【问题标题】:Scraping using Beautifulsoup, extracting text使用 Beautifulsoup 进行抓取,提取文本
【发布时间】:2020-05-27 16:43:58
【问题描述】:

我正在尝试从下面的行中提取“₹75”。

<td class="srpTuple__midGrid title_semiBold srpTuple__spacer16 " id="srp_tuple_price">₹ 75

我正在尝试提取“₹75”。有人可以帮忙吗? :)

【问题讨论】:

  • 你尝试了什么?
  • @sureshmani 我用这个:: rows=tags.findAll('td') 表示行中的行:if row.get('id',None)=='srp_tuple_price':
  • @Sureshmani rows=tags.findAll('td') for row in rows: if row.get('id',None)=='srp_tuple_price': 但在此之后受到打击! :(
  • 如果你尝试提取元素的值,那么应该使用row.text。请参阅下面的示例。

标签: python-3.x web-scraping beautifulsoup


【解决方案1】:

你可以试试看价格₹ 75:
对于单个td 元素:

html_doc = """<td class="srpTuple__midGrid title_semiBold srpTuple__spacer16 " id="srp_tuple_price">₹ 75</td"""

soup = BeautifulSoup(html_doc, 'lxml')

price = price = soup.find('td', id="srp_tuple_price").text

print(price)

对于多个td元素:

html_doc = """<td class="srpTuple__midGrid title_semiBold srpTuple__spacer16 " id="srp_tuple_price">₹ 75</td"""

soup = BeautifulSoup(html_doc, 'lxml')

prices = soup.find_all('td', id="srp_tuple_price")

for price in prices:
    print(price.text)

【讨论】:

    【解决方案2】:

    这将提取值并将其与变量进行比较,

    srp_tuple_price = '₹ 75'
    html = '<td class="srpTuple__midGrid title_semiBold srpTuple__spacer16 " id="srp_tuple_price">₹ 75</td>'
    soup = BeautifulSoup(html,features='html.parser')
    
    rows=soup.findAll('td')
    for row in rows: 
        if row.text==srp_tuple_price:
            print('Success')
        else:
            print ('fail')
    

    输出: 成功

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2019-02-15
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多