AttributeError:“NoneType”对象没有属性“encode using beautifulsoup”

2024-04-26 22:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,所以我面临这个问题:

#!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')

我收到此错误:AttributeError:“NoneType”对象没有属性“encode”

有什么帮助吗?在


Tags: thenameinimporturlresponsesyspage