1
votes

I am really new in Python, so I'm facing this issue:

#!venv/bin/python
import sys
import requests
import bs4

if len(sys.argv) == 3:
# If arguments are satisfied store them in readable variables
   url = 'http://%s' % sys.argv[1]
   file_name = sys.argv[2]

print('Grabbing the page...')
# Get url from command line
response = requests.get(url)
response.raise_for_status()

# Retrieve all links on the page
soup = bs4.BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a')

file = open(file_name, 'wb')
print('Collecting the links...')
for link in links:
    href = link.get("href") + '\n' 
    file.write(href.encode())
file.close()
print('Saved to %s' % file_name)

else:

print('Usage: ./collect_links.py www.example.com file.txt')

I am getting this error : AttributeError: 'NoneType' object has no attribute 'encode'

Any helps, please?

1
Without seeing the HTML, the best we can do is guess ... but ... I'd guess that you've got an <a> that does not have an href attribute, which means link.get("href" + '\n') returns None, and None doesn't have an encode() method. - g.d.d.c
This error almost always means one of two things: (a) you called some function that always returns None, like list.reverse, or (b) you called some function that usually returns a value but returns None when there is no value to get, like many find-type functions. - abarnert
From the traceback (which you didn't show us—please do in the future—but I can guess), the error comes from the file.write(href.encode()) line, so it's either file or href that's None. Since the error message is about encode rather than write, it must be href. So now, you just need to figure out what function you called to get href. If you don't understand why that function can return None, read the docs, and it'll probably explain. So then you just have to figure out what to do with those cases. - abarnert
Nice, well, I made a little change, so I believe is href is returning none - Edson Santos
for link in links: href = link.get('href') + '\n' file.write(href.encode()) file.close() - Edson Santos

1 Answers

0
votes

SOLVED!

I include this line : href = "{0}\n".format(link.get("href"))

Thank you, guys!