html 是否提取标签之间的嵌套字符串文本< font>?

gev0vcfq  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(181)

我正在尝试制作矿物+价格的列表。我成功地完成了第一步(它显示了第一页的矿物列表),但我无法获取价格值。我尝试了在StackOverflow上找到的一些其他方法(使用兄弟/父标记等),但我没有成功...另外,如果我使用两个“for”循环,您稍后可以将一个列表附加/添加到另一个列表(名称+价格)吗?
下面是我想找到的一个片段,它在标签之间。使用'font'查找对我来说不成功...我真的不需要“价格”文本,但我需要“€580 / US$598 / ¥84010 / AUD$890”。
第一个
`

zi8p0yeb

zi8p0yeb1#

试试这个:

import requests
from bs4 import BeautifulSoup

URL = "https://www.fabreminerals.com/search_results.php?LANG=EN&SearchTerms=&submit=Buscar&MineralSpeciment=&Country=&Locality=&PriceRange=&checkbox=enventa&First=0"

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
}

soup = BeautifulSoup(requests.get(URL, headers=headers).text, "lxml")

prices = [
    p.getText(strip=True).split("Price:")[-1] for p
    in soup.select("table tr td font font")
]
names = [n.getText(strip=True) for n in soup.select("table tr td font a")]

# Filter the lists
names[:] = [" ".join(n.split()) for n in names if not n.startswith("[")]
prices[:] = [p for p in prices if p]

# Print the results
for name, price in zip(names, prices):
    print(f"{name}\n{price}")
    print("-" * 50)

输出量:

NX51AH2: 'lepidolite' after Elbaite with Elbaite
€580 / US$598 / ¥84010 / AUD$890
--------------------------------------------------
TH27AL9: 'Pearceite' with Calcite
€220 / US$227 / ¥31860 / AUD$330
--------------------------------------------------
TFM69AN5: 'Stilbite'
€450 / US$464 / ¥65180 / AUD$690
--------------------------------------------------
SM90CEX: Acanthite
€90 / US$92 / ¥13030 / AUD$130
--------------------------------------------------
TMA97AN5: Acanthite
€240 / US$247 / ¥34760 / AUD$370
--------------------------------------------------
TB90AE8: Acanthite
€540 / US$557 / ¥78220 / AUD$830
--------------------------------------------------
TZ71AK9: Acanthite
€580 / US$598 / ¥84010 / AUD$890
--------------------------------------------------
EC63G1: Acanthite
€85 / US$87 / ¥12310 / AUD$130
--------------------------------------------------
MN56K9: Acanthite
€155 / US$159 / ¥22450 / AUD$230
--------------------------------------------------
TF89AL3: Acanthite (Se-bearing) with Polybasite (Se-bearing) and Calcite
€460 / US$474 / ¥66630 / AUD$700
--------------------------------------------------
TP66AJ8: Acanthite (Se-bearing) with Pyrite
€1500 / US$1547 / ¥217290 / AUD$2310
--------------------------------------------------
TY86AN2: Acanthite after Polybasite
€1600 / US$1651 / ¥231770 / AUD$2460
--------------------------------------------------
TA66AF6: Acanthite with Calcite
€160 / US$165 / ¥23170 / AUD$240
--------------------------------------------------
JFD104AO2: Acanthite with Calcite
€240 / US$247 / ¥34760 / AUD$370
--------------------------------------------------
TX36AL6: Acanthite with Calcite
€1200 / US$1238 / ¥173830 / AUD$1850
--------------------------------------------------
TA48AH1: Acanthite with Chalcopyrite
€290 / US$299 / ¥42000 / AUD$440
--------------------------------------------------
EF89L9: Acanthite with Pyrite and Calcite
€480 / US$495 / ¥69530 / AUD$740
--------------------------------------------------
TX89AN0: Acanthite with Siderite and Proustite
€4800 / US$4953 / ¥695320 / AUD$7400
--------------------------------------------------
EA56K0: Acanthite with Silver
€150 / US$154 / ¥21720 / AUD$230
--------------------------------------------------
EC48K0: Acanthite with Silver
€290 / US$299 / ¥42000 / AUD$440
--------------------------------------------------
11AT12: Acanthite, Calcite
€70 / US$72 / ¥10140 / AUD$100
--------------------------------------------------
9EF89L9: Acanthite, Pyrite, Calcite
€320 / US$330 / ¥46350 / AUD$490
--------------------------------------------------
SM75TDA: Adamite
€75 / US$77 / ¥10860 / AUD$110
--------------------------------------------------
2M14: Adamite
€90 / US$92 / ¥13030 / AUD$130
--------------------------------------------------
20MJX66: Adamite
€140 / US$144 / ¥20280 / AUD$215
--------------------------------------------------

相关问题