Home > Mobile >  doubleclick an element in beautifulsoup in python
doubleclick an element in beautifulsoup in python

Time:06-01

I can't get the text by using xpath in beautifulsoup but selenium can get that text by using doubleclick command. How can I get the element by using beautifulsoup?

I tried:

import requests
from lxml import etree
from bs4 import BeautifulSoup
#Function to Find the element from the Xpath
def Xpath(url):
  Dict_Headers = ({'User-Agent':
      'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
      (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',\
      'Accept-Language': 'en-US, en;q=0.5'})
  # Gets the requried data https browser's address bar
  webPage = requests.get(url,Dict_Headers)
  # Creating a soup Object from the html content
  Scraping = BeautifulSoup(webPage.content, "html.parser") 
  # Conveting Soup object to etree object for Xpath processing
  documentObjectModel = etree.HTML(str(Scraping)) 
  return (documentObjectModel.xpath("//div[@id='pages']/div/section/div[5]/div/div[3]/div[2]/div[2]")[0].text)
URL = "http://..."
print(Xpath(URL))

doubleclick command in selenium is that:

selenium.doubleClick("xpath=//div[@id='pages']/div/section/div[5]/div/div[3]/div[2]/div[2]")

The thing that I want to get is "0" from screenshot inspected:

The thing that I want to get is "0"

CodePudding user response:

You don't need neither selenium nor beautifulSoup for that. There's an API that the data is fetched from.

Here's how to get that part your XPath points to:

import requests

url = "https://servis.mgm.gov.tr/web/sondurumlar?merkezid=90601"
headers = {
    "Accept": "application/json, text/plain, */*",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:100.0) Gecko/20100101 Firefox/100.0",
    "Referer": "https://mgm.gov.tr/",
    "Origin": "https://mgm.gov.tr",
}

data = requests.get(url, headers=headers).json()[0]

for k, v in data.items():
    print(f"{k}: {v}")

Output:

aktuelBasinc: 911.6
denizSicaklik: -9999
denizeIndirgenmisBasinc: 1009
gorus: 20000
hadiseKodu: AB
istNo: 17130
kapalilik: 2
karYukseklik: -9999
nem: 25
rasatMetar: -9999
rasatSinoptik: -9999
rasatTaf: -9999
ruzgarHiz: 13.32
ruzgarYon: 36
sicaklik: 29.7
veriZamani: 2022-06-01T09:10:00.000Z
yagis00Now: 0
yagis10Dk: 0
yagis12Saat: 0
yagis1Saat: 0
yagis24Saat: 0
yagis6Saat: 0
denizVeriZamani: 2022-06-01T09:00:00.000Z
  • Related