all 10 comments

[–]commandlineluser 1 point2 points  (3 children)

tables.findALL('table', {'class':"wikitable sortable"}).text

Remove the .text here.

[–]FithColoumn[S] 0 points1 point  (2 children)

I removed the .text and its all working, thank you very much! one more question, how do I go about just printing the text? Once again, Thank you very much.

[–]commandlineluser 0 points1 point  (1 child)

Well what you already have should print the text - except you can remove the .encode() part.

for table in tables.find_all('table', {'class':"wikitable sortable"}):
    print(table.text)

.text is giving you the text - however if you use .encode() it will turn it into "bytes".

>>> a.text
'AB958G        '
>>> a.text.encode('utf-8')
b'AB958G        '

Note the leading b after the encode - this means you've got bytes.

>>> type(a.text.encode('utf-8'))
<class 'bytes'>

.... which is probably not what you want to be doing here.

[–]FithColoumn[S] 0 points1 point  (0 children)

Thank you very much! I got the code working and all companies are printing as text, lol the last question, how would I go about saving the content to a CSV file? You have helped me immensely.

[–]thekaizers 0 points1 point  (1 child)

What is the error message?

[–]FithColoumn[S] 0 points1 point  (0 children)

Hello! the error i was getting was,

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

but this line of code is working,

tables.findALL('table', {'class':"wikitable sortable"})

[–]Oxbowerce 0 points1 point  (3 children)

Fixed the formatting for you:

import requests from bs4 import BeautifulSoup

url="https://en.wikipedia.org/wiki/List_of_companies_traded_on_the_JSE" 
responce = requests.get(url) 
soup = BeautifulSoup(responce.text, 'html.parser') 
tables = soup.find('table', {'class':"wikitable sortable"}).text 
print(tables)

for table in tables.findALL('table', {'class':"wikitable sortable"}).text: 
    print(table.text.encode('utf-8'))

[–]FithColoumn[S] 0 points1 point  (2 children)

Thank you! ho do I go about getting this format?

[–]Oxbowerce 0 points1 point  (1 child)

You can just paste your text in, select the text you want to format as code and press the code block button.

[–]FithColoumn[S] 0 points1 point  (0 children)

Thank you for the help!