2017-04-21 55 views
0

我的python脚本使用BeautifulSoup,似乎无法把握页面上div的单词,是否有特定的原因呢?我可以抓取个人资料图片来计算消息的数量,但不是文本本身。使用BeautifulSoup刮掉whoscall.in上的问题4

(仅供参考,我已经使用这个页面:http://whoscall.in/1/2392247496/

if(website == "1"): 
    reqInput = "http://whoscall.in/1/%s/" % (teleWho) 
    urlfile = urllib2.Request(reqInput) 
    print (reqInput) 
    time.sleep(1) 
    requestRec = requests.get(reqInput) 
    soup = BeautifulSoup(requestRec.content, "lxml") 
    noMatch = soup.find(text=re.compile(r"no reports yet on the phone number")) 
    print(requestRec.content)# #only if needed# 
    type(noMatch) is str 
    if noMatch is None: 
    worksheet.write(idx+1, 2, "Got a hit") 
    howMany = soup.find_all('img',{'src':'/default-avatar.gif'}) 
    howManyAreThere = len(howMany) 
    worksheet.write(idx+1,1,howManyAreThere) 
    print (howManyAreThere) 
    scamNum = soup.find_all(text=("scam"),recursive=True) 
    #,'scam','Scammer','scammer'# 
    scamCount = len(scamNum) 
    print(scamNum) 
    searchTerms = {scamCount:scamCount} 
    sentiment = max(searchTerms, key=searchTerms.get) 
    worksheet.write(idx+1,3,sentiment) 

我似乎无法拉文本“骗局”关闭页面

我不确定它为什么拒绝找到那个文本,就像其他美丽的汤码一样完美。

https://github.com/GarnetSunset/Haircuttery/

回答

1

改变这一行:

scamNum = soup.find_all(text=("scam"),recursive=True) 

到:

scamNum = [ div.text for div in soup.find_all('div', {'style':'font-size:14px; margin:10px; overflow:hidden'}) if 'scam' in div.text.lower() ] 

试试这个多个单词:

words = [ 'word1', 'word2', ... ] 
scamNum = [ div.text for div in soup.find_all('div', {'style':'font-size:14px; margin:10px; overflow:hidden'}) if any(word for word in words if word in div.text.lower()) ] 
+0

完全奏效。你是最好的。 :) – GarnetSunset

+0

谢谢,很高兴我能帮到 –

+0

最后一件事,想你会喜欢这个。 https://github.com/GarnetSunset/Haircuttery/commit/93a12ab60eea9df36339a1378966f8f9fd0ecc78 – GarnetSunset