Kevin Johnson
Kevin Johnson

Reputation: 47

Python web scraping: TypeError: 'int' object is not subscriptable

url = "https://technet.microsoft.com/en-us/library/hh135098(v=exchg.150).aspx"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
table = soup.find_all('table', attrs={"responsive": "true"})[0]
for rows in table.find_all('tr')[1]:
    item = []
    for val in rows.find('td'):
        item.append(val.text.strip())
        print (item)

Traceback (most recent call last):
    File "<stdin>", line 3, in <module>
TypeError: 'int' object is not interable

Line 4 refers to for val in rows.find('td'):

Upvotes: 0

Views: 772

Answers (1)

JkShaw
JkShaw

Reputation: 1947

In for val in rows.find('td'): when there's no td found in the rows, it returns -1 an int object which you are trying to loop, hence the error.

The correct approach:

>>> for rows in table.find_all('tr'):
...   item = []
...   for val in rows.find_all('td'):
...     item.append(val.text.strip())
...   print(item)
... 
[]
['Exchange Server 2016 CU5', 'March 21, 2017', '15.01.0845.034']
['Exchange Server 2016 CU4', 'December 13, 2016', '15.01.0669.032']
['Exchange Server 2016 CU3', 'September 20, 2016', '15.01.0544.027']
['Exchange Server 2016 CU2', 'June 21, 2016', '15.01.0466.034']
['Exchange Server 2016CU1', 'March 15, 2016', '15.01.0396.030']
['Exchange Server 2016 RTM', 'October 1, 2015', '15.01.0225.042']
['Exchange Server 2016 Preview', 'July 22, 2015', '15.01.0225.016']

Upvotes: 1

Related Questions