我想在谷歌主页上统计这个词。但是我在初始阶段得到了AttributeError。
我的代码是 - >
import requests
from bs4 import BeautifulSoup
import operator
def main(url):
word_list=[]
source_code=requests.get(url).text
soup=BeautifulSoup(source_code,'lxml')
for post_text in soup.findAll('a'):
content=post_text.string
words=content.lower().split()
for each_word in words:
print(each_word)
word_list.append(each_word)
main('https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=2-nqWavnB4WN8Qf4n7eQAw')
我的输出是 - >
images
maps
play
youtube
news
gmail
drive
Traceback (most recent call last):
File "word_freq.py", line 18, in <module>
main('https://www.google.co.in/?gfe_rd=cr&dcr=0&ei=2-nqWavnB4WN8Qf4n7eQAw')
File "word_freq.py", line 13, in main
words=content.lower().split()
AttributeError: 'NoneType' object has no attribute 'lower'
答案 0 :(得分:0)
您正在使用html解析网页,因此您需要
汤= BeautifulSoup(source_code, 'html.parser')
string是获取任何标记内容的错误属性,请使用text:
含量= post_text.text