skip to Main Content

I’am new in web-parsing and I ran into a problem.
I wanna extract number of watching from the site:
enter image description here

I wrote this code for that purpose:

url = 'https://www.kommersant.ru/doc/4638344'
request = requests.get(url)
req = request.text
soup=BeautifulSoup(req, 'html')
abcd = soup.find("div", class_="doc_sharing__body js-social").find("span", class_="sharing")
print(abcd)

But result of this query is "None"

What’s the problem? Please, help!

I try many iterations with different variation of my code (for example):

url = 'https://www.kommersant.ru/doc/4638344'
request = requests.get(url)
req = request.text
soup=BeautifulSoup(req, 'html')
abcd =  soup.find_all("span", attr = {'class':'sharing'}
print(abcd)

But I have same result!

2

Answers


  1. I believe that something like this script can help you retrieve the number of views (I am assuming that this number is the one inside the title tag):

    import requests
    from bs4 import BeautifulSoup
    
    url = "https://www.kommersant.ru/doc/4638344"
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    
    sharing_div = soup.find("div", class_="doc_sharing__body")
    
    title = sharing_div.find("span", class_="sharing")["title"]
    
    watching_number = title.split(": ")[1]
    
    print(watching_number)
    

    Note: Maybe you’ll have to use .find_all(), instead of .find(). In this case, you have to inspect the retrieved results, to check which of them contains the desired title tag.

    Login or Signup to reply.
  2. The view count is stored in the data-article-views= parameter:

    import requests
    from bs4 import BeautifulSoup
    
    url = 'https://www.kommersant.ru/doc/4638344'
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    
    for article in soup.select('article[data-article-views]'):
        print(article['data-article-title'], article['data-article-views'])
    

    Prints:

    «Газпром» начал поставлять газ в Сербию по «Турецкому потоку» 14541
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search