Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

python - Python-使用Beautifulsoup从网页提取数据(Python - Extracting data from web page using Beautifulsoup)

I am trying to scrape some data from a webpage using bs4 Given below is what I have done thus far,

(我正在尝试使用bs4从网页上抓取一些数据鉴于以下是我到目前为止所做的事情,)

import requests
from bs4 import BeautifulSoup


url = 'www.website.com'
response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

for article in soup.find_all('section'):
    print(article)

The above code returns the below output:

(上面的代码返回以下输出:)

<section>
<ul class="row-full-width" style="margin:0; list-style: none; padding-left: 0; font-size: 120%">
<li class="four columns">

  Comp A:

  <i class="icon-rupee"></i>
<b>136.90</b>

  Cr.


 </li>
<li class="four columns">

  Comp B:

  <i class="icon-rupee"></i>
<b>10.95</b>
</li>
<li class="four columns">

  Comp C:
  <i class="icon-rupee"></i> <b>49.60</b> / <b>10.20</b>
</li>
<li class="four columns">

  Comp D:

  <i class="icon-rupee"></i>
<b>6.61</b>
</li>
<li class="four columns">

  Comp E:

  <b>25.78</b>
</li>
<li class="four columns">

  Comp F:

  <b>0.00</b>

  %


</li>
<li class="four columns">

  Comp G:

  <b>9.39</b>

  %


</li>
<li class="four columns">

  Comp H:

  <b>6.54</b>

  %


 </li>
<li class="four columns">

  Comp I:

  <b>19.39</b>

  %


</li>
<li class="four columns">

I am trying to extract each of the Comp's and their corresponding values:

(我试图提取每个Comp及其对应的值:)

Expected Output :

(预期产量:)

Comp A,136.90 Cr
Comp B, 10.95
Comp C, 49.60/10.20
Comp D, 6.61
Comp E, 25.78
Comp F, 0.0%
Comp G, 9.39%
Comp H, 6.54%
Comp I, 19.39%
  ask by scott martin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use get_text() method with separator= parameter and then split the string.

(您可以使用带有separator=参数的get_text()方法,然后分割字符串。)

For example ( data contains your HTML string):

(例如( data包含您的HTML字符串):)

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print(soup.prettify())

for li in soup.select('li'):
    row = li.get_text(strip=True, separator='|').split('|')
    col1, col2 = row[0].replace(':', ''), ' '.join(row[1:])
    print('{:<20}{:<20}'.format(col1, col2))

Prints:

(印刷品:)

Comp A              136.90 Cr.          
Comp B              10.95               
Comp C              49.60 / 10.20       
Comp D              6.61                
Comp E              25.78               
Comp F              0.00 %              
Comp G              9.39 %              
Comp H              6.54 %              
Comp I              19.39 %  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...