0

I couldn't find the value. the value location in html is (span 161 /span) Should I try xpath method to get theelement ? Or better suggetion ?

!pip install Selenium
from selenium import webdriver

# 要把chromedriver 放在同一層資料夾裡
browser = webdriver.Chrome(executable_path='./chromedriver.exe')

browser.get("https://shopee.tw/shop/10228173/search?page=0&sortBy=ctime")
source = browser.page_source

import time
import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(source)
browser.get("https://www.rakuten.com.tw/shop/watsons/product/956957/")
soup = BeautifulSoup(browser.page_source)
soup
products =[]
product = {}
#     product['網址'] = link

product['購買次數'] = soup.find('div',class_="b-container-child").span
products.append(product)
print(products)
    
DDYY
  • 5
  • 1
  • Can you show your expected output – Bhavya Parikh Nov 27 '21 at 06:04
  • If your idea is to find the items by class name you can refer to this post: https://stackoverflow.com/questions/5041008/how-to-find-elements-by-class – Kheng Nov 27 '21 at 06:13
  • output '購買次數': 樂天點數}] But the value isn't what I need. I need the value : 161 in the page. How could I use the corecct code to get the value ? Thanks. – DDYY Nov 27 '21 at 06:13

1 Answers1

1

You can use headers to get full data from soup and also you have to select which element you have to find so from b-container-child we have to select 2nd index and from it we have to find last span tag and then it will return 161 as output.

import requests
from bs4 import BeautifulSoup
headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"}
res=requests.get("https://www.rakuten.com.tw/shop/watsons/product/956957/",headers=headers)

soup=BeautifulSoup(res.text,"html.parser")
all_data=soup.find_all("div",class_="b-container-child")[2]
main_data=all_data.find_all("span")[-1]
print(main_data.text)

Output:

161
Bhavya Parikh
  • 3,304
  • 2
  • 9
  • 19
  • 2
    Thanks a lot. I see if I use span to get all data, then pick up the correct value step by step, I can get that. It's really helpful ! – DDYY Nov 27 '21 at 06:54