python - 如何使用 beautifulsoup 从 HTML 中提取数据

标签 python html web-scraping beautifulsoup

我正在尝试抓取网页并将结果存储在 csv/excel 文件中。我正在为此使用 BeautifulSoup 。

我正在尝试使用 find_all 函数从 soup 中提取数据,但我不确定如何捕获字段名称或标题中的数据

HTML 文件具有以下格式

<h3 class="font20">
 <span itemprop="position">36.</span> 
 <a class="font20 c_name_head weight700 detail_page" 
 href="/companies/view/1033/nimblechapps-pvt-ltd" target="_blank" 
 title="Nimblechapps Pvt. Ltd."> 
     <span itemprop="name">Nimblechapps Pvt. Ltd. </span>
</a> </h3>

到目前为止,这是我的代码。不确定如何从这里开始

from bs4 import BeautifulSoup as BS
import requests 
page = 'https://www.goodfirms.co/directory/platform/app-development/iphone? 
page=2'
res = requests.get(page)
cont = BS(res.content, "html.parser")
names = cont.find_all(class_ = 'font20 c_name_head weight700 detail_page')
names = cont.find_all('a' , attrs = {'class':'font20 c_name_head weight700 
detail_page'})

我试过使用以下 -

Input: cont.h3.a.span
Output: <span itemprop="name">Nimblechapps Pvt. Ltd.</span>

我想提取公司名称 - “Nimblechapps Pvt. Ltd。”

最佳答案

您可以为此使用列表理解:

from bs4 import BeautifulSoup as BS
import requests

page = 'https://www.goodfirms.co/directory/platform/app-development/iphone?page=2'
res = requests.get(page)
cont = BS(res.content, "html.parser")
names = cont.find_all('a' , attrs = {'class':'font20 c_name_head weight700 detail_page'})
print([n.text for n in names])

您将获得:

['Nimblechapps Pvt. Ltd.', (..) , 'InnoApps Technologies Pvt. Ltd', 'Umbrella IT', 'iQlance Solutions', 'getyoteam', 'JetRuby Agency LTD.', 'ONLINICO', 'Dedicated Developers', 'Appingine', 'webnexs']

关于python - 如何使用 beautifulsoup 从 HTML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53924719/

相关文章:

python - K 均值结果索引在第二次运行中有所不同

python - Django:N个应用程序共享1个数据库

javascript - 如何创建持久的随机倒计时?

python - 使用 BeautifulSoup 抓取网站并保持不变的 URL

app-store - 获取应用商店数据

python - Scrapy CrawlSpider : how to access item across different levels of parsing

python - 如何找到非卷积模型tensorflow python的概率

Python——如何从互联网上抓取图像

javascript - 通过input type = file获取字节数组

html - CSS 不适用于 HTML 页面