2014-03-19 21 views
0

代碼正在運行並插入,但在命令提示符中出現錯誤,說'tab' is not recognised as an internal or external command,operable program or batch file選項卡未被識別爲內部或外部命令錯誤?

我做了什麼錯誤,我該如何解決? 這裏是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。網址中的標籤正在產生問題。我如何解決它?

+0

在哪一行發生此錯誤? – Ofiris

+0

你的腳本可能只是缺少shebang行。作爲第一行插入'#!/ usr/bin/env python'並重試。 – Alfe

+0

你如何運行這個程序?你在使用什麼操作系統? –

回答

0

你所得到的錯誤是一個Windows的錯誤,而不是一個Python的錯誤。不知何故,一個或兩個os.system調用都將「tab」作爲命令傳遞給Windows命令行。

我懷疑這是因爲google.com網頁上的許多網址都有?tab = Wx或& tab = wT或其他類似的網址。這個?不應該傷害任何東西,但&可能會被解釋爲另一個命令的開始。 (如果是這樣的話,我希望你收到的錯誤不僅僅是'標籤')。

2

使用subprocess.call()方法,而不是os.system()

在URL中的&是什麼原因造成的問題。

在Windows上:

Command1 & Command2 

意味着運行COMMAND1然後運行命令2

相關問題