|
由于我们不是所有的信息都要,所以我们可以一个一个的提取,最终输出个数据。代码如下(这中间用到了前面提到的选择器知识和一些字符串处理方法):
- # 定义第4个函数parse_content_2,用来解析并匹配第二层网页内容,并输出数据,此处使用BeautifulSoup方法
- def parse_content_2(response,content):
-
- # 使用bs4进行爬取第二层信息
- soup = BeautifulSoup(response)
-
- # 爬取发行地和收益分配方式,该信息位于id为procon1下的table下的第4个tr里
- tr_3 = soup.select('#procon1 > table > tr')[3]
- address = tr_3.select('.pro-textcolor')[0].text
- r_style = tr_3.select('.pro-textcolor')[1].text
-
- # 爬取发行规模,该信息位于id为procon1下的table下的第5个tr里
- tr_4 = soup.select('#procon1 > table > tr')[4]
- guimo = tr_4.select('.pro-textcolor')[1].text
- re_2 = re.compile(r'.*?(d+).*?', re.S)
- scale = re_2.findall(guimo)[0]
- # 爬取收益率,该信息位于id为procon1下的table下的第8个tr里
- tr_7 = soup.select('#procon1 > table > tr')[7]
- rate = tr_7.select('.pro-textcolor')[0].text[:(-1)]
- r = rate.split('至')
- r_min = r[0]
- r_max = r[1]
-
- # 提取利率等级
- tr_11 = soup.select('#procon1 > table > tr')[11]
- r_grade = tr_11.select('p')[0].text
-
- # 保存数据到一个字典中
- item = {
- '产品名称':content[1],
- '发行机构':content[2],
- '发行时间':content[3],
- '产品期限':content[4],
- '投资行业':content[5],
- '首页收益':content[6],
- '发行地': address,
- '收益分配方式': r_style,
- '发行规模': scale,
- '最低收益': r_min,
- '最高收益': r_max,
- '利率等级': r_grade
- }
-
- # 返回数据
- return item
(编辑:网站开发网_安阳站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|