python解析html,提取注释部分

在这里插入图片描述

from bs4 import BeautifulSoup,Comment
import requests
def get_name(url):
    test = {'company_name': [],'code':[],'city':[],'industry':[],'register':[],'income':[],'profit':[]}
    req = requests.get(url, headers=headers, verify=False)
    soup = BeautifulSoup(req.text, 'html.parser')
    if soup.find_all('ul', {'class': 'hot-search clear'}):
        for i in soup.find_all('ul', {'class': 'hot-search clear'}):
            if i.find_all('div', {'class': 'inf'}):
                for j in i.find_all('div', {'class': 'inf'}):
                    name = j.find('h2').text   #公司名
                    test['company_name'].append(name)
                    test['code'].append(code)
                    info = (j.findAll(text=lambda text: isinstance(text, Comment)))[1]
                    info = BeautifulSoup(info, 'html.parser')
                    for s in info.find_all('p'):
                    if s.text == '注册资金':
                        register=s.find_next('span').text
                        test['register'].append(register)
                    if s.text == '营业收入':
                        income = s.find_next('span').text
                        test['income'].append(income)
                    if s.text == '净利润':
                        profit = s.find_next('span').text
                        test['profit'].append(profit)
    return test
info = get_name('https://www.ccotc.cn/Enterprise/index/cid/69/lid/84/industryid/C/cityid/10/businessIncomeId/A/order/4/p/2.html')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值