Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Urllib从用户收集url时显示语法错误_Python_Python 3.x_Beautifulsoup_Urllib - Fatal编程技术网

Python Urllib从用户收集url时显示语法错误

Python Urllib从用户收集url时显示语法错误,python,python-3.x,beautifulsoup,urllib,Python,Python 3.x,Beautifulsoup,Urllib,我正在用Python编写一个简单的web抓取程序,该程序使用urllib和BeautifulSoup收集web文档中链接的名称 import urllib from bs4 import BeautifulSoup # Not link used in project. Just a sample URL html = urllib.urlopen('http://stackoverflow.com/index.html').read() soup = BeautifulSoup(html,

我正在用Python编写一个简单的web抓取程序,该程序使用urllib和BeautifulSoup收集web文档中链接的名称

import urllib
from bs4 import BeautifulSoup

# Not link used in project. Just a sample URL
html = urllib.urlopen('http://stackoverflow.com/index.html').read()
soup = BeautifulSoup(html, 'html.parser')
count = int(input("Enter Count: "))
position = int(input("Enter Position: "))
tags = soup("a")
当我在代码中以普通字符串的形式键入url时,如上图所示(在“urllib.urlopen”),一切都很正常

但在这部法典中:

import urllib
from bs4 import BeautifulSoup

link = str(input("Enter URL: "))
html = urllib.urlopen(link).read()
soup = BeautifulSoup(html, 'html.parser')
count = int(input("Enter Count: "))
position = int(input("Enter Position: "))
tags = soup("a")
键入输入时,由于URL中的冒号,出现语法错误:

link = str(input("Enter URL: "))
File "<string>", line 1
http:stackoverflow.com
    ^
SyntaxError: invalid syntax
link=str(输入(“输入URL:”)
文件“”,第1行
http:stackoverflow.com
^
SyntaxError:无效语法
我不知道为什么URL中的冒号会导致语法错误


如何修复此问题?

您似乎正在Python 2解释器中运行此Python 3代码输入尝试逐字计算用户提供的输入。更改您用来修复此问题的解释器。我只安装了Python3,而且我的解释器也使用Python3。这是我在问这个问题之前检查的第一件事。或者,如果必须使用Python 2,请使用
raw\u input()
而不是
input()
。我会检查四倍。我认为在Python3中不可能出现这种错误。我会这样做。谢谢您似乎正在Python2解释器中运行此Python3代码输入尝试逐字计算用户提供的输入。更改您用来修复此问题的解释器。我只安装了Python3,而且我的解释器也使用Python3。这是我在问这个问题之前检查的第一件事。或者,如果必须使用Python 2,请使用
raw\u input()
而不是
input()
。我会检查四倍。我认为在Python3中不可能出现这种错误。我会这样做。谢谢