如何使用Beautiful Soup将这些文本保留在标签中?

时间:2019-06-26 06:31:19

标签: python web-scraping beautifulsoup

我想从上面的代码中提取数字121。但是我得到的汤对象没有显示数字。

Link to my Image

    [<div class="open_pln" id="pln_1">
 <ul>
 <li>
 <div class="box_check_txt">
 <input id="cp1" name="cp1" onclick="change_plan(2,102,2);" type="checkbox"/>
 <label for="cp1"><span class="green"></span></label>
 </div>
 </li>
 <li id="li_open"><span>Desk</span> <br/></li>
 <li> </li>
 </ul>
 </div>]

3 个答案:

答案 0 :(得分:1)

开放式办公室的数字121不在HTML代码中,而在JavaScript中。您可以使用regex提取它:

import re
import requests

url ='https://www.coworker.com/search/los-angeles/ca/united-states'

htmlpage = requests.get(url).text

open_offices = re.findall(r'var openOffices\s*=\s*(\d+)', htmlpage)[0]
private_offices = re.findall(r'var privateOffices\s*=\s*(\d+)', htmlpage)[0]

print('Open offices: {}'.format(open_offices))
print('Private offices: {}'.format(private_offices))

打印:

Open offices: 121
Private offices: 40

答案 1 :(得分:1)

没有re模块:

import requests
from bs4 import BeautifulSoup

url ='https://www.coworker.com/search/los-angeles/ca/united-states'

res = requests.get(url)
soup = BeautifulSoup(res.text,"lxml")
searchstr = "var openOffices = "
script = soup.select_one(f"script:contains('{searchstr}')").text
print(script.split(searchstr)[1].split(";")[0])

输出:

121

答案 2 :(得分:0)

您必须使用这样的汤来查找所有li属性-

    attribute=req["li"]       
    all_links = soup.find_all(attribute)
    for link in all_links:
        print(link.text.strip())