标签未被识别为内部或外部命令错误?

0 投票
2 回答
776 浏览
提问于 2025-04-17 22:56

代码正在运行并插入数据,但我在命令提示符中看到错误信息,内容是 'tab' 不是内部或外部命令,也不是可运行的程序或批处理文件

我犯了什么错误,应该怎么修复呢?以下是我的Python代码:

updatedb.py

import sqlite3 as db
import urllib
import re
import sys
url=sys.argv[1]
htmltext=urllib.urlopen(url).read()
regex='<title>(.+?)</title>'
pattern=re.compile(regex)
title= re.findall(pattern,htmltext)
print title[0]
id="1"
conn=db.connect('insertlinks.db')
cursor=conn.cursor()
with conn:
    cursor.execute('insert into records (id,keyword) values(?,?)',(id,title[0]))
#print "inserted"
#conn.close()

上面的代码是这样调用的:

import urlparse
import os
import urllib
from bs4 import BeautifulSoup
url="http://www.google.com"
urls=[url]
visited=[url]
try:
    while len(urls)>0:
        htmltext=urllib.urlopen(urls[0]).read()
        soup=BeautifulSoup(htmltext)
        urls.pop(0)
        for tag in soup.findAll('a',href=True):
            tag['href']=urlparse.urljoin(url,tag['href'])
            if tag['href'] not in urls and tag['href'] not in visited:
                os.system("python scraper/insertlinks.py %s"  % (tag['href']))
                os.system("python scraper/updatedb.py %s" % (tag['href']))
                urls.append(tag['href'])
                visited.append(tag['href'])
except:
    print 'error in 1'

补充说明:问题出在 tag['href']。它的值是 http://maps.google.co.in/maps?hl=en&tab=il。这个网址中的“tab”导致了问题。我该怎么解决呢?

2 个回答

2

subprocess.call() 方法来代替 os.system()

网址中的 & 符号就是导致问题的原因。

在Windows系统上:

Command1 & Command2

这表示先执行命令1,然后再执行命令2。

0

你遇到的这个错误是Windows系统的问题,而不是Python的问题。看起来你的os.system调用中,有一个或两个是把“tab”当成命令传给了Windows的命令行。

我猜这可能是因为很多google.com页面上的网址后面都有类似于?tab=Wx或&tab=wT这样的参数。这种问号(?)应该不会造成什么问题,但这个和号(&)可能会被当作另一个命令的开始。(如果真是这样的话,我觉得你应该会收到更多关于其他内容的错误,而不仅仅是“tab”的错误。)

撰写回答