使用Beautiful Soup从HTML Span提取数据

时间:2018-11-08 17:47:56

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

我想从html代码中提取“ 1.02千万”和“ 7864”,并将它们保存在csv文件的不同列中。

代码:

<div class="featuresvap _graybox clearfix"><h3><span><i class="icon-inr"></i>1.02 Crores</span><small> @ <i class="icon-inr"></i><b>7864/sq.ft</b> as per carpet area</small></h3>

enter image description here

2 个答案:

答案 0 :(得分:0)

不确定实际数据,但这只是我很快就提出来的内容。如果需要它导航到网站,请使用import requests。您需要添加url = 'yourwebpagehere' page = requests.get(url)并将soup更改为soup = BeautifulSoup(page.text, 'lxml'),然后删除html变量,因为它是不必要的。

from bs4 import BeautifulSoup
import csv

html = '<div class="featuresvap _graybox clearfix"><h3><span><i class="icon-inr"></i>1.02 Crores</span><small> @ <i class="icon-inr"></i><b>7864/sq.ft</b> as per carpet area</small></h3>'
soup = BeautifulSoup(html, 'lxml')
findSpan = soup.find('span')
findB = soup.find('b')
print([findSpan.text, findB.text.replace('/sq.ft', '')])

with open('NAMEYOURFILE.csv', 'w+') as writer:
    csv_writer = csv.writer(writer)
    csv_writer.writerow(["First Column Name", "Second Column Name"])
    csv_writer.writerow([findSpan, findB])

答案 1 :(得分:0)

用代码自我解释

from bs4 import BeautifulSoup

# data for first column
firstCol = []
# data for second column
secondCol = []

for url in listURL:
    html = '.....' # downloaded html
    soup = BeautifulSoup(html, 'html.parser')

    # 'select_one' select using CSS selectors, return only first element
    fCol = soup.select_one('.featuresvap h3 span')
    # remove: <i class="icon-inr"></i>
    span.find("i").extract()
    sCol = soup.select_one('.featuresvap h3 b')
    firstCol.append(fCol.text)
    secondCol.append(sCol.text.replace('/sq.ft', ''))

with open('results.csv', 'w') as fl:
    csvContent = ','.join(firstCol) + '\n' + ','.join(secondCol)
    fl.write(csvContent)

''' sample results
1.02 Crores | 2.34 Crores
7864        | 2475

'''
print('finish')