[英]Parameter passing in function
多年來,Python 似乎已經解決了這個問題,但無論如何:
def soupstrainer(tag_element,srch_str):
''' take a soup element
return a list of found items
'''
results=[]
###literal search string returns results, even though two lines down,
### print(srch_str) returns the expected string
souper=tag_element.find_all('a',{'data-tn-element':'companyName'}) #srch_str)
print(srch_str)
for r in souper:
if r != None:
results.append(r.get_text(r.string, strip=True))
return results
with open('scrapesnip.html', 'r') as the_file:
doc4 = the_file.read()
soup = BeautifulSoup(doc4, 'html.parser')
result = soupstrainer(soup,str("'a',{'data-tn-element':'companyName'}"))
print(result,len(result))
結果:
## zero results passing the string
/PYscripts/bravosierra4.py
'a',{'data-tn-element':'companyName'} <=== these two strings *look* identical
[] 0
## with the identical string
## 'hard coded' into the function
/PYscripts/bravosierra4.py
'a',{'data-tn-element':'companyName'} <=== these two strings *look identical
['Keysight Technologies', 'ECS Federal LLC', 'Corsica Technologies, LLC', 'Caribou', 'Collins Aerospace', 'Travelers', 'CyberCoders', 'HealthVerity', 'Circadence Corporation'] 9
我是否錯誤地傳遞srch_string
?
我不確定你到底是如何傳遞srch_string
的,但是這個:
souper = tag_element.find_all('a', {'data-tn-element': 'companyName'})
與此不同:
srch_string = "'a', {'data-tn-element': 'companyName'}"
souper = tag_element.find_all(srch_string)
在第一種情況下,您將一個字符串和一個字典作為單獨的 arguments 傳遞。 在第二種情況下,您傳遞的是單個字符串。
看起來 BeautifulSoup 將 find_all 解析為自然列表,因此當它顯示為字符串時,不會返回任何內容。 這是我成功編碼調用 function 的行的方式:
result = soupstrainer(soup,['a',{'data-tn-element':'companyName'}])
並像這樣重新編碼 function:
def soupstrainer(tag_element,srch_str):
''' take a soup element
return a list of found items
'''
results=[]
souper=tag_element.find_all(srch_str[0],srch_str[1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.