为了账号安全,请及时绑定邮箱和手机立即绑定

Beautiful Soup - 在带有字符串的标签中找到一个标签?第n个孩子?

Beautiful Soup - 在带有字符串的标签中找到一个标签?第n个孩子?

料青山看我应如是 2022-06-02 18:08:31
我在使用以下 HTML Scrape 时遇到了一些问题 res =   <div class="gunDetails">    <h4>Specifications</h4>    <ul class="features">        <li><label>Make:</label><span itemprop="brand">Gamo</span></li>        <li><label>Model:</label><span itemprop="model">Coyote Black Tactical</span></li>        <li><label>Licence:</label><span>No Licence</span></li>        <li><label>Orient.:</label><span>Ambidextrous</span></li>        <li><label>Scope:</label><span>Unknown&nbsp;3-9x32</span></li>        <li><label>Origin:</label><span>Spanish</span></li>        <li><label>Cased:</label><span>Other</span></li>        <li><label>Trigger:</label><span>1</span></li>        <li><label>Condition:</label><span itemprop="itemCondition">Used</span></li>    </ul>  </div>我正在尝试将文本放入它自己的单独变量中,以便我可以将其导出到带有我自己的标题的 CSV。每当我这样做时,我都可以将它们全部放在一个字符串中,或者根本没有。soup = BeautifulSoup(res, 'html.parser')gun_details = soup.select_one('div.gunDetails')for tag in gun_details or []:  for tag in gun_details.select("li"):   for tag in gun_details.select('span'):      print(tag.text)输出GamoCoyote Black TacticalNo LicenseAmbidextrousUnknown 3-9x32SpanishOther1Used无论如何,我可以为每个标签文本创建一个变量吗?就像是?gun_make = gun_details.findAll('label', String="Make:")print(gun_make).text
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

让我们试试这个:


res =  """ <div class="gunDetails">

    <h4>Specifications</h4>

    <ul class="features">

        <li><label>Make:</label><span itemprop="brand">Gamo</span></li>

        <li><label>Model:</label><span itemprop="model">Coyote Black Tactical</span></li>

        <li><label>Licence:</label><span>No Licence</span></li>

        <li><label>Orient.:</label><span>Ambidextrous</span></li>

        <li><label>Scope:</label><span>Unknown&nbsp;3-9x32</span></li>

        <li><label>Origin:</label><span>Spanish</span></li>

        <li><label>Cased:</label><span>Other</span></li>

        <li><label>Trigger:</label><span>1</span></li>

        <li><label>Condition:</label><span itemprop="itemCondition">Used</span></li>

    </ul>

  </div>

""


from bs4 import BeautifulSoup as bs

import csv


labels = []

spans = []

soup = bs(res, 'html.parser')

gun_details = soup.select_one('div.gunDetails')

for l in gun_details.select('label'):

    labels.append(l.text.replace(':',''))

for s in gun_details.select('span'):

    spans.append(s.text)


my_dict = dict(zip(labels, spans))

with open('mycsvfile.csv','w') as csvfile:

        writer = csv.DictWriter(csvfile, fieldnames=None)

        for key in my_dict.keys():

            csvfile.write(f"{key},{my_dict[key]}\n")

输出:


Make    Gamo

Model   Coyote Black Tactical

Licence No Licence

Orient. Ambidextrous

Scope   Unknown 3-9x32

Origin  Spanish

Cased   Other

Trigger 1

Condition   Used


查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信