首页 > 解决方案 > 为什么我在输入 url 后得到一个回溯说“无效参数”?

问题描述

输入的文件名:http://www.dr-chuck.com

Traceback (most recent call last):
  File "C:\Users\Sree\Desktop\py4e\crawl.py", line 1, in <module>
    import urllib.request, urllib.parse, urllib.error
  File "C:\Users\Sree\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 86, in <module>
    import email
  File "C:\Users\Sree\Desktop\py4e\email.py", line 2, in <module>
    fh = open(fname)
OSError: [Errno 22] Invalid argument: 'http://www.dr-chuck.com'

我输入的任何网址都被视为无效参数!请帮忙!

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter: ') 
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')

# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
    print(tag.get('href', None))

标签: pythonpython-3.xbeautifulsouptraceback

解决方案


推荐阅读