2016-11-08 82 views
0

我正在學習從網絡中抓取文本。香港專業教育學院寫了下面的功能python傳遞包含引號的參數

from bs4 import BeautifulSoup 
import requests 

def get_url(source_url): 
    r = requests.get(source_url) 
    data = r.text 
    #extract HTML for parsing 
    soup = BeautifulSoup(data, 'html.parser') 
    #get H3 tags with class ... 
    h3list = soup.findAll("h3", { "class" : "entry-title td-module-title" }) 
    #create data structure to store links in 
    ulist = [] 
    #pull links from each article heading 
    for href in h3list: 
     ulist.append(href.a['href']) 
    return ulist 

我從一個單獨的文件調用此...

from print1 import get_url 

ulist = get_url("http://www.startupsmart.com.au/") 

print(ulist[3]) 

的問題是,我使用的CSS選擇器是相當獨特的,以我解析該網站。所以這個功能有點「脆弱」。我想如果我添加參數傳遞給函數的定義

def get_url(source_url, css_tag): 

的CSS選擇器作爲參數傳遞給函數

,並試圖通過"h3", { "class" : "entry-title td-module-title" }

它spazzes出

TypeError: get_url() takes exactly 1 argument (2 given)

我試圖轉義所有引號,但它仍然無法正常工作。

我真的很感謝一些幫助。我無法找到這個先前的答案。

+0

你sured你定義'高清GET_URL(SOURCE_URL,css_tag):'正確,這樣做,再次導入更改的功能之後?看起來你的函數定義變化沒有反映在你調用'get_url()'的文件中' –

+0

你的錯誤不是關於引號,而是關於'get_url()'函數,如上所述,它只需要(正好)一個論據。 – maze88

+0

我認爲你確定的這個問題是正確的。我需要重新啓動python,以便在添加它之後識別第二個參數。 –

回答

0

這裏是可用的版本:

from bs4 import BeautifulSoup 
import requests 

def get_url(source_url, tag_name, attrs): 
    r = requests.get(source_url) 
    data = r.text 
    # extract HTML for parsing 
    soup = BeautifulSoup(data, 'html.parser') 
    # get H3 tags with class ... 
    h3list = soup.findAll(tag_name, attrs) 
    # create data structure to store links in 
    ulist = [] 
    # pull links from each article heading 
    for href in h3list: 
     ulist.append(href.a['href']) 
    return ulist 

ulist = get_url("http://www.startupsmart.com.au/", "h3", {"class": "entry-title td-module-title"}) 

print(ulist[3]) 
+0

這工作。我真的不明白爲什麼,但謝謝 –

+0

@AxleMax你不瞭解它嗎?你似乎認爲你可以通過引用多個引號將多個值合併成一個值,但它根本就不行。所以我只通過了不同的值。 –