Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

python - Exit Code 0 but no output

I am new to python and coding in general. I found a code to scrape a website but whenever I run that code all I get is Exit Code 0 (I know that is good as it means there are not mistakes) but I do not get any output. Following the code I am using.

Does anyone have any ideas how to solve this ?

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/")
question from:https://stackoverflow.com/questions/65855868/exit-code-0-but-no-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you are trying to search for a class entry-content but it seems that there is no class in your HTML you are getting from URL. Go to URL and inspect that page and find the class for which you are looking and update the line at below line in your code:

for each_text in my_soup.findAll('div', {'class':'entry-content'}):

In your code, you never go inside this for loop, and the rest functions are not called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...