【问题标题】:Scrape Text Between Two Strong Tags在两个强标签之间刮取文本
【发布时间】:2022-08-16 21:40:21
【问题描述】:

我正在尝试从以下网站抓取“167”(右上角):https://www.goodfirms.co/billing-invoicing-software/

我可以设法获取所有文本,但我只是想获取数字,我不确定如何隔离它。有人可以帮助我吗?

代码:

from bs4 import BeautifulSoup as bs
import requests
import requests_random_user_agent

s = requests.Session()
user_agent = s.headers[\'User-Agent\']

headers = {
\'accept\': \'*/*\',
\'accept-language\': \'en-GB,en-US;q=0.9,en;q=0.8,es;q=0.7,ru;q=0.6\',
\'referer\': \'https://www.google.com/\',
\'connection\': \'Keep-alive\',
\'user-agent\': user_agent,
}

response = requests.get(\'https://www.goodfirms.co/billing-invoicing-software/\', headers=headers)

soup = bs(response.content, \'lxml\')

test = soup.find(\"section\", class_=\"section-breadcrumb blog-breadcrumb overflow\").text

print(test)

输出:

Home >
Billing and Invoicing Software
167 Softwares  |  Last updated: Jul 31, 2022

期望的输出:

167

    标签: web-scraping beautifulsoup


    【解决方案1】:

    数字167 位于class="last_update 内部<strong> 标签内的标签下:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = "https://www.goodfirms.co/billing-invoicing-software/"
    
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    
    num = soup.select_one(".last_update strong")
    print(num.text)
    

    印刷:

    167
    

    【讨论】:

    • 甜的!因此,在此处添加“强”会告诉它在强标签之间查找。我必须阅读“select_one”。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多