jerrythebum
jerrythebum

Reputation: 362

Python 2 Beautiful Soup, get text from all tags

Trying to get the text from all tags that have the class task-topic-deprecated, however I only seem to be able to get one.

Not a duplicate of BeautifulSoup get_text from find_all - This issue uses multiple class names and so the working syntax is slightly different, class_ as opposed to attrs={'class':'

Source page: https://developer.apple.com/documentation/cfnetwork?language=objc

The output would be any string that is struckout on the page above:

CFFTPCreateParsedResourceListing
kCFFTPResourceGroup
...etc

find_next() doesn't seem to move to the next item how I am expecting it to, and prints out the text I have already.

page = requests.get("https://developer.apple.com/documentation/cfnetwork?language=objc")
soup = BeautifulSoup(page.content, 'html.parser')

aRow = soup.find('a', attrs={'class':'task-topic-deprecated has-adjacent-element symbol-name'}).get_text()
print aRow
bRow = soup.find('a', attrs={'class':'task-topic-deprecated has-adjacent-element symbol-name'}).find_next().get_text()
print bRow
cRow = soup.find('a', attrs={'class':'task-topic-deprecated has-adjacent-element symbol-name'}).find_next().find_next().get_text()
print cRow


CFFTPCreateParsedResourceListing
CFFTPCreateParsedResourceListing
CFFTPCreateParsedResourceListing

Also tried putting it in a loop from various things I have found on Stack Overflow, but it seems to still only grab 1 item as per above.

Also tried with xPath, but this doesn't grab anything and prints out a blank list

tree = html.fromstring(page.content)
allItems = tree.xpath('//a[@class="task-topic-deprecated has-adjacent-element symbol-name"]/text()')
print allItems 

Upvotes: 0

Views: 71

Answers (1)

utks009
utks009

Reputation: 573

I think you have doing it wrong instead of find you can use find_all method to get result.

for i in soup.find_all('a', class_='task-topic-deprecated has-adjacent-element symbol-name'):
    print i.get_text()

May be this could help

Upvotes: 1

Related Questions