-1

htmlの部分はこんな感じで、

<div id="block-hubs3d-hub-hub-specialties" class="block block-hubs3d-hub first odd">
        <h3 class="block-title">Specialties</h3>

<div class="field field-name-field-hub-specialties field-type-taxonomy-term-reference field-label-hidden">
    <div class="field-items">
          <div class="field-item item-1 even">ABS+PLA+Nylon+Flexible</div>
          <div class="field-item item-2 odd">Custom Finishing</div>
          <div class="field-item item-3 even">DLP - SLA Technology</div>
          <div class="field-item item-4 odd">Makerjuice G+</div>
      </div>
</div>

フォーマットとして取得する方法、例えば:

specialties: ABS+PLA+Nylon+Flexible, Custom Finishing, DLP - SLA Technology, DLP - SLA Technology

これまでのところ、すべてのテキストを取得するために bs4 を使用することしか知りません:

response = requests.get('https://www.3dhubs.com/new-york/hubs/peerless')
soup = bs4.BeautifulSoup(response.text)
4

1 に答える 1

2

で を検索divclassます。

import bs4

h = """
<div id="block-hubs3d-hub-hub-specialties" class="block block-hubs3d-hub first odd">
        <h3 class="block-title">Specialties</h3>

<div class="field field-name-field-hub-specialties field-type-taxonomy-term-reference field-label-hidden">
    <div class="field-items">
          <div class="field-item item-1 even">ABS+PLA+Nylon+Flexible</div>
          <div class="field-item item-2 odd">Custom Finishing</div>
          <div class="field-item item-3 even">DLP - SLA Technology</div>
          <div class="field-item item-4 odd">Makerjuice G+</div>
      </div>
</div>
"""

b = bs4.BeautifulSoup(h)

specialties = [div.text for div in b.findAll("div", {"class":"field-item"})]
print(", ".join(b))

出力:

ABS+PLA+Nylon+Flexible, Custom Finishing, DLP - SLA Technology, Makerjuice G+
于 2015-04-16T13:33:47.377 に答える