我正在尝试提取span_id内的文本,但使用python beautifulsoup得到空白输出

时间:2019-04-18 06:43:08

标签: python-3.x beautifulsoup

我正在尝试在span-id标记内提取文本,但输出屏幕空白。

我也尝试过使用父元素div文本,但提取失败,请任何人帮助我。 下面是我的代码。

 import requests
 from bs4 import BeautifulSoup

 r = requests.get('https://www.paperplatemakingmachines.com/')
 soup = BeautifulSoup(r.text,'lxml')
 mob = soup.find('span',{"id":"tollfree"})
 print(mob.text)

我要给该跨度内的文本指定手机号码。

3 个答案:

答案 0 :(得分:1)

您必须使用Selenium,因为初始请求中不存在该文本,或者至少在没有搜索<script>标签的情况下没有文本。

from bs4 import BeautifulSoup as soup
from selenium import webdriver
import time

driver = webdriver.Chrome('C:\chromedriver_win32\chromedriver.exe')

url='https://www.paperplatemakingmachines.com/'
driver.get(url)

# It's better to use Selenium's WebDriverWait, but I'm still learning how to use that correctly
time.sleep(5)

soup = BeautifulSoup(driver.page_source, 'html.parser')
driver.close()

mob = soup.find('span',{"id":"tollfree"})
print(mob.text)

答案 1 :(得分:0)

数据实际上是通过脚本动态发送的。您需要做的是从脚本中解析数据:

mCursorAdapter = new EventCursorAdapter(this, cursor);

答案 2 :(得分:0)

使用正则表达式查找数字的另一种方法

Dismissible