猿问

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

我在使用以下 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)

输出


Gamo

Coyote Black Tactical

No License

Ambidextrous

Unknown 3-9x32

Spanish

Other

1

Used

无论如何,我可以为每个标签文本创建一个变量吗?就像是?


gun_make = gun_details.findAll('label', String="Make:")

print(gun_make).text


料青山看我应如是
浏览 78回答 1
1回答

慕村225694

让我们试试这个:res =&nbsp; """ <div class="gunDetails">&nbsp; &nbsp; <h4>Specifications</h4>&nbsp; &nbsp; <ul class="features">&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Make:</label><span itemprop="brand">Gamo</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Model:</label><span itemprop="model">Coyote Black Tactical</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Licence:</label><span>No Licence</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Orient.:</label><span>Ambidextrous</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Scope:</label><span>Unknown&nbsp;3-9x32</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Origin:</label><span>Spanish</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Cased:</label><span>Other</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Trigger:</label><span>1</span></li>&nbsp; &nbsp; &nbsp; &nbsp; <li><label>Condition:</label><span itemprop="itemCondition">Used</span></li>&nbsp; &nbsp; </ul>&nbsp; </div>""from bs4 import BeautifulSoup as bsimport csvlabels = []spans = []soup = bs(res, 'html.parser')gun_details = soup.select_one('div.gunDetails')for l in gun_details.select('label'):&nbsp; &nbsp; labels.append(l.text.replace(':',''))for s in gun_details.select('span'):&nbsp; &nbsp; spans.append(s.text)my_dict = dict(zip(labels, spans))with open('mycsvfile.csv','w') as csvfile:&nbsp; &nbsp; &nbsp; &nbsp; writer = csv.DictWriter(csvfile, fieldnames=None)&nbsp; &nbsp; &nbsp; &nbsp; for key in my_dict.keys():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvfile.write(f"{key},{my_dict[key]}\n")输出:Make&nbsp; &nbsp; GamoModel&nbsp; &nbsp;Coyote Black TacticalLicence No LicenceOrient. AmbidextrousScope&nbsp; &nbsp;Unknown 3-9x32Origin&nbsp; SpanishCased&nbsp; &nbsp;OtherTrigger 1Condition&nbsp; &nbsp;Used
随时随地看视频慕课网APP

相关分类

Java
我要回答