简体   繁体   中英

Python BeautifulSoup4 only shows tag but does not show the text

I am practicing web scraping using BeautifulSoup4 to scrape data from a website. However, when I run my code, it only shows the opening tag and ending tag when there is clearly a text between them when I do inspect element on the website.

My code:

import requests
from bs4 import BeautifulSoup as bs

url = 'https://emenscr.nesdc.go.th/thaime/loanact/view.html?id=43k11nkm0VFwVVOZxlX9'
r = requests.get(url).text
soup = bs(r,'lxml')

p_name = soup.find('span', id='viewer_loanact_group')
print(p_name)

Output:

In [54]: runcell(0, 'D:/computer_vision_project/bpm/untitled3.py')
<span id="viewer_loanact_group"></span>

The html:

<tr>                                                               
<th>กลุ่มของข้อเสนอโครงการ</th>
 <td>
  <span id="viewer_loanact_group">ข้อเสนอระดับโครงการย่อยที่มีวัตถุประสงค์เพื่อฟื้นฟูเศรษฐกิจและสังคม ตามบัญชีแนบท้าย พ.ร.ก. เงินกู้ฯ (กลุ่มที่ 3)
  </span>
 </td>
</tr>

chromedriver download link: https://chromedriver.chromium.org/downloads

**unzip and copy the .exe file to a folder

import time
from selenium import webdriver

chrome_driver_path = "**path of the folder/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)


url = 'https://emenscr.nesdc.go.th/thaime/loanact/view.html?id=43k11nkm0VFwVVOZxlX9'
driver.get(url)
time.sleep(10)
p_name = driver.find_element_by_xpath('//*[@id="viewer_loanact_group"]')
print(p_name.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM