Advertisement
Guest User

exfkalatestpost

a guest
Apr 7th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. # Set the URL of the blog page to scrape
  5. url = "http://www.ezfka.com/"
  6.  
  7. # Set the user agent string
  8. user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
  9.  
  10. # Set the headers with the user agent string
  11. headers = {"User-Agent": user_agent}
  12.  
  13. # Send a GET request to the blog page with the headers
  14. response = requests.get(url, headers=headers)
  15.  
  16. # Parse the HTML content using Beautiful Soup
  17. soup = BeautifulSoup(response.content, 'html.parser')
  18.  
  19. # Find the first post element
  20.  
  21. post = soup.find("article")
  22.  
  23. # Get the URL of the post
  24. post_url = post.find('a')['href']
  25.  
  26. # Print the post URL
  27. print(post_url)
  28.  
  29.  
  30. # Retrieve the HTML code of the WordPress post's page
  31. url = post_url
  32. response = requests.get(url, headers=headers)
  33. html = response.text
  34.  
  35. # Parse the HTML code using Beautiful Soup
  36. soup = BeautifulSoup(html, 'html.parser')
  37.  
  38. # Find the HTML element that contains the comments section of the post
  39. comments_section = soup.find("div", {"id":"wpd-threads"})
  40.  
  41. # Create an empty dictionary to store user IDs and their reply counts
  42. reply_counts = {}
  43.  
  44. # Find all the HTML elements that represent each user
  45. user_id_elements = comments_section.findAll("div", {"class":"wpd-comment-author"})
  46.  
  47. # Loop through all comments and count replies by each user
  48. for user_id_element in user_id_elements:
  49.  
  50. if user_id_element:
  51. username = user_id_element.text
  52. if username in reply_counts:
  53. reply_counts[username] += 1
  54. else:
  55. reply_counts[username] = 1
  56.  
  57. # Print the reply counts by each user
  58. print("There are a total of " + str(sum(reply_counts.values())) + " comments.")
  59. for user_id, count in reply_counts.items():
  60. print(f"User {user_id} made {count} comments.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement