2017-05-14 67 views
0
**keyword = '' 

'''To obtain keyword''' 
def test():`enter code here` 
    keywords = list() 
    while True: 
     print('what do you want to do?(a: add a key word for searching, q:quit adding words and start)') 
     command = input('command:') 
     if command == 'a': 
      word = input('keyword: ') 
      if word not in keywords: 
       keywords.append(word) 
     elif command == 'q': 
      break 
     else: 
      print('please input a valid command') 
    if len(keywords) == 0: 
     return 
    search_string = '' 
    for keyword in keywords: 
     search_string += keyword 
     search_string += '+' 
    search_string = search_string[:-1] 
    print(search_string) 

    search_url = 'http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-' \ 
       'bool.html&r=0&f=S&l=50&TERM1=' + search_string + '&FIELD1=&co1=AND&TERM2=&FIELD2=&d=PTXT' 

    return search_url 
'''Incoming url start the scrapy crawle''' 
class Uspto(scrapy.Spider): 
    name = 'uspto' 
    #allowed_domains = ['http://patft.uspto.gov/'] 
    #start_url = 'http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=0&f=S&l=50&TERM1=water&FIELD1=&co1=AND&TERM2=&FIELD2=&d=PTXT' 

    allowed_domains = ["http://patft.uspto.gov"] 
    keyword = test() 

    start_urls = [ 
     #"http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=0&f=S&l=50&TERM1=python&FIELD1=&co1=AND&TERM2=&FIELD2=&d=PTXT", 
     keyword, 

    ] 
** 

enter image description here錯誤截圖無法獲得關鍵字蟒蛇scrapy履帶

從鍵盤輸入的關鍵字,然後啓動爬行,現在的問題是我的關鍵字方法問題訪問

回答

0

根據您的錯誤信息NameError: name 'a' is not defined,似乎你正在使用的不是蟒蟒3 2,如果是的話,使用raw_input()代替input()

command = raw_input("commands:") 

raw_input()返回用戶輸入的字符串,你可以參考this answer瞭解inputraw_input更多細節。

+0

非常感謝您回答我的問題。這真的是一個版本問題。謝謝 – Matcha00

+0

如果是解決方案,請將其標記爲接受答案,謝謝:) –