2017-04-07 18 views
0

我有以下程序,在其中我想傳遞一個元素,以連續的谷歌搜索的列表:如何把所有的單詞列表中的元素作爲變量

search_terms = ['Telejob (ETH)', 'Luisa da Silva','The CERN Recruitment Services'] 
for el in search_terms: 
    webpage = 'http://google.com/search?q='+el) 
    print('xxxxxxxxxxxxxxxxxxx') 
    print(webpage) 

不幸的是我的計劃是不把所有的單詞每個列表項,但只取第一個,給我這樣的輸出:

http://google.com/search?q=Telejob (ETH) 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=Luisa da Silva 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=The CERN Recruitment Services 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=The Swiss National Science Foundation 

Altough你可以看到每一個字整個項目被添加到上面的搜索,當我驗證鏈接,它將僅作爲元素連接每個項目的第一個單詞,例如:

http://google.com/search?q=Telejob 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=Luisa 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=The 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=The 

我在做什麼錯,有什麼解決方案將每個列表項中的所有單詞連接到谷歌搜索?

謝謝

+0

參見[這裏](http://stackoverflow.com/questions/19353368/passing -string變量與 - 空格)。不同的語言,相同的問題,相同的解決方 –

回答

0

這條線:

webpage = 'http://google.com/search?q='+el) 

應拆分並加入%20木匠:

webpage = 'http://google.com/search?q='+'%20'.join(el.split())) 
+0

我是新手。通過上面的Evans Murithi的urllib使用這種方法有什麼缺點? – skeitel

+0

我的解決方案專門處理空間。它比URL lib解決方案的覆蓋範圍更小,但不需要導入...我只是直接回答問題,沒有更多的上下文......根據提問者的需求,任何答案都可能是正確的。 – JacobIRR

0

您可以在python3中使用urllib.parse.urlencode。對於python2,你可以使用urllib.urlencode

import urllib 

search_terms = ['Telejob (ETH)', 'Luisa da Silva','The CERN Recruitment Services'] 
for el in search_terms: 
    query = urllib.parse.urlencode({'q': el}) # urllib.urlencode({'q': el}) 
    webpage = 'http://google.com/search?{}'.format(query) 
    print('xxxxxxxxxxxxxxxxxxx') 
    print(webpage) 
+0

我是新來的。使用這種方法比JacobIRR描述的聯合方法有什麼優點? – skeitel

+0

假設你的字符串中有特殊字符'ñ'ç',使用連接'+'不會對它進行編碼。 'urllib'會將其編碼爲'q =%C3%B1%C2%B4%C3%A7' –

0

這些答案都不能解決基本問題:您需要將整個字符串編碼爲url。

我選擇urllib.quote()

>>> import urllib 
>>> for term in search_terms: 
    print urllib.quote(term) 
Telejob%20%28ETH%29 
Luisa%20da%20Silva 
The%20CERN%20Recruitment%20Services 

通知的()也被編碼,如將任何其他奇怪的字符可能博克您的查詢。

在你的情況,這將是:

webpage = 'http://google.com/search?q=' + urllib.quote(el)) 

等值PY3:

from urllib import parse 
for term in search_terms: 
    print(parse.quote(term)) 

所以

webpage = 'http://google.com/search?q=' + parse.quote(el)) 
+0

回溯(最近一次調用最後一次): 文件「C:/Users/SK/PycharmProjects/untitled/another_temperase.py」 ,第13行,在 print(urllib.quote(el)) AttributeError:module'urllib'沒有屬性'quote' – skeitel

+0

@skeitel for Py3,你需要'from urllib import parse'並使用'parse .quote()'代替或使用[urllib.parse.urlencode()](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode),正如Evans Murithi解釋的 – TemporalWolf

+0

@skeitel在Python 3中,它不是'urllib.quote()',而是'url.parse.urlencode()' –

0

事情就是URL必須百分比編碼,網址中有特殊含義的字符,例如:

  • #:去到某個位置在頁面
  • /:我想你知道這個做什麼?

您應該使用quote()來解決這個問題,而只記得:

  • urllib.quote()是Python2
  • url.parse.quote()是Python3

下面是Python3一些例子:

from urllib.parse import quote 


quote('/bars/will/stay/intact') 
#'/bars/will/stay/intact' 

quote('/bars/wont/stay/intact', safe='') 
#'%2Fbars%2Fwont%2Fstay%2Fintact' #Actually, everything will be encoded here 

quote('()ñ´ ç') 
#'%28%29%C3%B1%C2%B4%20%C3%A7' 

所以,你的代碼是現在:

search_terms = ['Telejob (ETH)', 'Luisa da Silva','The CERN Recruitment Services'] 
for el in search_terms: 
    webpage = 'http://google.com/search?q='+quote(el) 
    print('xxxxxxxxxxxxxxxxxxx') 
    print(webpage) 

由於search_terms可能包括不會被quote('something')轉義其他字符,你將不得不使用它的安全參數:

search_terms = ['Telejob (ETH)', 'Luisa da Silva','The CERN Recruitment Services'] 
for el in search_terms: 
    webpage = 'http://google.com/search?q='+quote(el, safe='') 
    print('xxxxxxxxxxxxxxxxxxx') 
    print(webpage) 

這最後一個,輸出:

xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=Telejob%20%28ETH%29 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=Luisa%20da%20Silva 
xxxxxxxxxxxxxxxxxxx 
http://google.com/search?q=The%20CERN%20Recruitment%20Services 

我建議你看看:https://docs.python.org/3/library/urllib.parse.html#url-quoting進一步的信息(見? !一個#字符)

0

谷歌查詢的格式爲https://www.google.com/search?q=keyword_1+...+keyword_N所以你應該格式化你的查詢,像這樣:

search_terms = ["Telejob (ETH)", "Luisa da Silva","The CERN Recruitment Services"] 
for search_term in search_terms: 
    query = "+".join(search_term.split()) 
    url = "http://google.com/search?q=" + query 
相關問題