1
我是BeautifulSoup和python開發人員的全新手,我想爲我的個人網站自動化一些代碼。用BeautifulSoup和Python語法選擇,輸入和解析?
我有這樣的:
#!/usr/bin/env python
""" Test menu for Website
"""
import urllib2
from bs4 import BeautifulSoup
print (47 * '-')
print (" C H O I C E L I S T")
print (47 * '-')
print ("1. Page One")
print ("2. Page Two")
print ("3. Page Three")
print ("4. Page Four")
print (47 * '-')
print (47 * '-')
#############################
## Robust error handling ##
## only accpet int ##
#############################
## Wait for valid input in while...not ###
is_valid=0
while not is_valid :
try :
choice = int (raw_input('Enter your choice [1-8] : '))
is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
except ValueError, e :
print ("'%s' is not a valid choice." % e.args[0].split(": ")[1])
### Take action as per selected choice list option ###
if choice == 1:
print ("www.mywebsite.com/page_one.html")
elif choice == 2:
print ("www.mywebsite.com/page_two.html")
elif choice == 3:
print ("www.mywebsite.com/page_three.html")
elif choice == 4:
print ("www.mywebsite.com/page_four.html")
else:
print ("Invalid choice. try again...")
print (47 * '-')
print (47 * '-')
username = raw_input("Please, type your username\n")
html_content = urllib2.urlopen("http://" + [choice] + "/" + username)
soup = BeautifulSoup(html_content, "lxml")
#####################
## STRINGS REPLACE ##
#####################
start_msg = "Hey, you have "
end_msg = "comments !"
end_str = "read !"
####################
## COMMENTS COUNT ##
####################
count_comments = soup.find("span", "sidebar-comments")
count_comments
count_comments_final = count_comments.find_next("meta")
################
## COUNT READ ##
################
count_read = soup.find("span", "sidebar-read")
count_read
count_read_final = count_read.find_next("meta")
##################
## PRINT RESULT ##
##################
print start_msg + count_comments_final['content'].split(':')[1] + end_msg
print start_msg + count_read_final['content'].split(':')[1] + end_str
有了這個劇本,我想:
1 - 選擇我的網頁上(選擇列表 - 4)
2 - 輸入自己的用戶名
3 - 解析我選擇的網頁,並獲得所有評論和所有閱讀的計數。
我的問題是這裏html_content = urllib2.urlopen("http://" + [choice] + username)
,我無法檢索一個好的URL所需的參數!
請幫我找到正確的語法!
我最後的URL應該是:http://www.mywebsite.com/page_one.html/username
你能不能給我完整的代碼,我不知道在好地方來代替。我獲得此: 回溯(最近最後一次通話): 文件「./testTwo.py」 46行,在 頁=映射[選擇] NameError:名字‘選擇’沒有定義 –
TwinyTwice
2014-09-26 05:32:49
噢,對不起,我忘記說,非常感謝您的幫助;-) 作爲初學者很抱歉! – TwinyTwice 2014-09-26 05:48:30
我嘗試了很多不同的事情,我認爲你的新代碼合乎邏輯,但我仍然有同樣的錯誤:NameError:name'choice'沒有被定義,我不明白!抱歉 – TwinyTwice 2014-09-26 06:04:36