Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

beautifulsoup - Extracting data from html using python

I am trying to extract the text within all the 'a' tags from this code, i get an error:

(AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?)

What am i doing wrong?

import json
import time
from datetime import datetime
from discord import Webhook, RequestsWebhookAdapter
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from bs4 import BeautifulSoup
from discord.ext import commands

r = requests.get("https://www.hypedc.com/nz/nike-air-force-1-07-black-black-cw2288-001")
soup = BeautifulSoup(r.content, 'lxml')
size = soup.find('div', id="size-selector-tab-mobile-0")
size = size.find_all('li', class_='col-xs-6 col-sm-8 col-md-6 col-lg-4')
size = size.find_all('a').text
print(size)
question from:https://stackoverflow.com/questions/65621734/extracting-data-from-html-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here size.find_all('li', class_='col-xs-6 col-sm-8 col-md-6 col-lg-4') return a list. So you need to iterate it to get the size.

import requests
r = requests.get("https://www.hypedc.com/nz/nike-air-force-1-07-black-black-cw2288-001")
soup = BeautifulSoup(r.content, 'lxml')
size = soup.find('div', id="size-selector-tab-mobile-0")

size = size.find_all('li') # return list

for t in size:
    print(t.a.text.strip())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...