Python Forum

Full Version: Can't Resolve Webscraping AttributeError
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

The following code is meant to get table for "IFPI 2014 data" and put it in a csv file.
Although the csv file is being created but there is no data in it.

import csv
import urllib.request
from bs4 import BeautifulSoup

f = open('dataoutput.csv', 'w', newline = '')
writer = csv.writer(f)
soup = BeautifulSoup(urllib.request.urlopen("https://en.wikipedia.org/wiki/Global_music_industry_market_share_data").read(), 'lxml')

tbody = soup('table', {"class":"wikitable plainrowheaders sortable"})[0].find_all('tr')
for row in tbody:
    cols = row.findChildren(recursive=False)
    cols = [ele.text.script() for ele in cols]
    writer.writerow(cols)
    print(cols)
I keep getting this error message

Traceback (most recent call last):
  File "C:\Users\Hass\eclipse-workspace\FTS\src\Webscraping.py", line 18, in <module>
    cols = [ele.text.script() for ele in cols]
  File "C:\Users\Hass\eclipse-workspace\FTS\src\Webscraping.py", line 18, in <listcomp>
    cols = [ele.text.script() for ele in cols]
AttributeError: 'str' object has no attribute 'script'
Can someone please point out where I am going wrong and how to fix it.

Thanks
What would you expect a text element's script to contain?
What output/errors do you get if you use the text element itself?