我对python和一般的编码都是新手。我找到了一个代码来刮网站,但每当我运行该代码时,我得到的都是退出代码0(我知道这很好,因为这意味着没有错误),但我没有得到任何输出。遵循我正在使用的代码
有人知道如何解决这个问题吗
from collections import Counter
import requests
from bs4 import BeautifulSoup
def my_start(url):
my_wordlist = []
my_source_code = requests.get(url).text
my_soup = BeautifulSoup(my_source_code, 'html.parser')
for each_text in my_soup.findAll('div', {'class':'entry-content'}):
content = each_text.text
words = content.lower().split()
for each_word in words:
my_wordlist.append(each_word)
clean_wordlist(my_wordlist)
# Function removes any unwanted symbols
def clean_wordlist(wordlist):
clean_list =[]
for word in wordlist:
symbols = '!@#$%^&*()_-+={[}]|\;:"<>?/., '
for i in range (0, len(symbols)):
word = word.replace(symbols[i], '')
if len(word) > 0:
clean_list.append(word)
create_dictionary(clean_list)
def create_dictionary(clean_list):
word_count = {}
for word in clean_list:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
c = Counter(word_count)
# returns the most occurring elements
top = c.most_common(10)
print(top)
# Driver code
if __name__ == '__main__':
my_start("https://www.tutorialspoint.com/python3/python_overview.htm/")
我在我的计算机上运行了你的代码。整个for循环(如下所述)未运行
这是因为该页面上有“entry content”div,因此不会调用其余函数。当我提到网站代码中的一个div类时,该代码可以很好地用于其他web url
下次尝试使用调试器或使用打印语句调试代码。祝你好运
您正试图搜索一个类
entry-content
,但您从URL
获得的HTML
中似乎没有类。转到URL
并检查该页面,找到要查找的类,并更新代码中下一行的行:在代码中,您永远不会进入这个for循环,并且不会调用其余函数
相关问题 更多 >
编程相关推荐