gpt4 book ai didi

python - Beautiful Soup - 在带有字符串的标签中找到标签?第 n 个 child ?

转载 作者:行者123 更新时间:2023-12-01 07:14:39 25 4
gpt4 key购买 nike

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

这是完整的代码:

from bs4 import BeautifulSoup
import requests
import csv

all_links=[]
labels = []
spans = []
url="https://www.guntrader.uk/dealers/redcar/spencers-sporting-guns/guns?page={}"

for page in range(1,3):
res=requests.get(url.format(page)).text
soup=BeautifulSoup(res,'html.parser')
for link in soup.select('a[href*="/dealers/redcar/spencers-sporting-guns/guns/shotguns"]'):
all_links.append("https://www.guntrader.uk" + link['href'])


print(len(all_links))
for a_link in all_links:
res = requests.get(a_link).text
soup = BeautifulSoup(res, 'html.parser')
gun_details = soup.select('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('gundealer.csv','w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=None)
for key in mydict.keys():
csvfile.write(f"{key},{my_dict[key]}\n")

这部分似乎可以单独工作,给出正确的输出:

from bs4 import BeautifulSoup
import requests

response = request.get('guntrader.uk/guns/air-rifles/gamo/pre-charged-pneumatic/22/coyote-black-tactical-190920182249936')

soup = BeautifulSoup(response.content, 'html.parser')
gun_details = soup.select_one('div.gunDetails')
gun_make = gun_details.select_one('li:nth-child(1)')
print (gun_make.text)

输出:

Make: Gamo

但我不知道我在做什么来扰乱循环的初始响应,以不允许上面的代码片段不起作用

最佳答案

让我们试试这个:

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

关于python - Beautiful Soup - 在带有字符串的标签中找到标签?第 n 个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58036089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com