2013-02-16 36 views
1

我正在使用機械化。我的代碼工作得很好,如果我在CMD控制檯運行,但在Visual Studio IDE中,我「爲+不支持的操作數類型(S):‘實例’和‘STR’」得到這個錯誤在行br.open('http://gmail.com')用於visual studio的python工具不接受//

我的代碼是

import mechanize 
import cookielib 
from BeautifulSoup import BeautifulSoup 
import html2text 

# Browser 
br = mechanize.Browser() 

# Cookie Jar 
cj = cookielib.LWPCookieJar() 
br.set_cookiejar(cj) 

# Browser options 
br.set_handle_equiv(True) 
br.set_handle_gzip(True) 
br.set_handle_redirect(True) 
br.set_handle_referer(True) 
br.set_handle_robots(False) 

# Follows refresh 0 but not hangs on refresh > 0 
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 

# User-Agent (this is cheating, ok?) 
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] 

# The site we will navigate into, handling it's session 
br.open('http://gmail.com') 

# Select the first (index zero) form 
br.select_form(nr=0) 

# User credentials 
br.form['Email'] = 'user' 
br.form['Passwd'] = 'password' 

# Login 
br.submit() 

# Filter all links to mail messages in the inbox 
all_msg_links = [l for l in br.links(url_regex='\?v=c&th=')] 
# Select the first 3 messages 
for msg_link in all_msg_links[0:3]: 
    print msg_link 
    # Open each message 
    br.follow_link(msg_link) 
    html = br.response().read() 
    soup = BeautifulSoup(html) 
    # Filter html to only show the message content 
    msg = str(soup.findAll('div', attrs={'class': 'msg'})[0]) 
    # Show raw message content 
    print msg 
    # Convert html to text, easier to read but can fail if you have intl 
    # chars 
# print html2text.html2text(msg) 
    print 
    # Go back to the Inbox 
    br.follow_link(text='Inbox') 

# Logout 
br.follow_link(text='Sign out') 

好像//不被接受?

有什麼想法嗎?

+1

錯誤引用了'+'操作...我在您發佈的代碼中看不到任何內容。我建議發佈真正的代碼以及完整的追溯。 – FatalError 2013-02-16 06:18:44

+0

我添加了真實的代碼。 – John 2013-02-16 06:34:49

+1

@John顯示完整的追溯,否則我們該怎麼知道錯誤發生在哪裏? – Bakuriu 2013-02-16 08:59:46

回答

0

很可能,從Visual Studio啓動它時,您使用的是錯誤的Python解釋器版本。轉到項目屬性(右鍵單擊解決方案資源管理器中的項目節點,然後選擇「屬性」),然後檢查常規選項卡上的解釋器設置。

相關問題