代码之家  ›  专栏  ›  技术社区  ›  Nathan123

如何在数据绑定之间刮取文本?

  •  0
  • Nathan123  · 技术社区  · 6 年前

    我正在尝试删除html代码中的名称

    <h3><a data-bind="'attr': { 'href': PersonURL }, 'text': PersonName" 
        href="/bios/mbaxter">Michael N. Baxter</a></h3>
    

    我的密码如下

    url="https://www.morganlewis.com/our-people-results?pagenum=1&sortingqs=Last%20name&pagesize=500&currentGroup=36ef4ad43dea406895fa2d41af32fada&filtergroup=Office&loadCategories=true&param_sitecontentcategory=OUR%20PEOPLE&schoolsearchstring=villanova&subCatInfo=Office,36ef4ad43dea406895fa2d41af32fada&subCatText=Office%20%3A%20Philadelphia"
    tag='h3'
    cls="data-bind"
    def name_scrape(url,tag,cls):
        page=requests.get(url) 
        soup=BeautifulSoup(page.content,'html.parser')
        #print(soup.prettify())
        find_name=soup.find_all(tag,class_=cls)
        for entry in find_name:
           print(entry)
    
    name_scrape(url,tag,cls)
    

    名称似乎在类“data bind”中。我怎样才能确保我能擦掉这个名字?

    1 回复  |  直到 6 年前
        1
  •  3
  •   SIM    6 年前

    那个网站的内容是动态的。所以,你有两个选择:要么使用任何浏览器模拟器 selenium 或者使用包含json数据的正确url。后者无疑是最好的方法。

    这就是你如何抓住它们(简单的方法):

    import requests
    
    url = "https://www.morganlewis.com/biosearchnew/execute?pagenum=1&isInternalBioRequest=false&SortingField=Last%20name&currentGroup=36ef4ad43dea406895fa2d41af32fada&loadCategories=true&param_sitecontentcategory=OUR%20PEOPLE&pagesize=500&schoolsearchstring=villanova&personofficeitem_sm=36ef4ad43dea406895fa2d41af32fada"
    
    res = requests.get(url)
    for items in res.json()['SearchResults']:
        print(items['Title'])
    

    部分响应:

    Lindsay Ann Barci
    Michael N. Baxter
    Jeannine T. Bishop
    Jeffrey P. Bodle
    Sarah E. Bouchard
    Brandon J. Brigham
    Amanda M. Bruno
    Evan W. Busteed