Python Urllib从用户收集url时显示语法错误
我正在用Python编写一个简单的web抓取程序,该程序使用urllib和BeautifulSoup收集web文档中链接的名称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,
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代码
raw\u input()
而不是input()
。我会检查四倍。我认为在Python3中不可能出现这种错误。我会这样做。谢谢您似乎正在Python2解释器中运行此Python3代码raw\u input()
而不是input()
。我会检查四倍。我认为在Python3中不可能出现这种错误。我会这样做。谢谢