【问题标题】:How to get to data-id inside li tag using beautiful soup?如何使用漂亮的汤获取 li 标签内的数据 ID?
【发布时间】:2020-08-08 05:21:28
【问题描述】:
<li class="result-card job-result-card result-card--with-hover-state job-card__contents--active" data-id="1946843843" data-entity-urn="urn:li:jobPosting:1946843843" data-search-id="e2f62398-982f-463e-9685-9064dfa810eb" data-tracking-id="UDB/ibIoQvCk1l54VneerA==" data-column="1" data-row="1">

我在 job_posting 中有单独的容器,我正在尝试访问 data-id 并获取编号...

这是我的代码

job_postings 中的工作:

        #Scrapping info from main search page
        
        try:
            job_id = job.find_all("li",{"class"})['data-id']
            
        except:
            job_id = 'No data available'
        job_ids.append(job_id)

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    你可以使用下面的方法

    from bs4 import BeautifulSoup
    html = '''<li class="result-card job-result-card result-card--with-hover-state job-card__contents--active" data-id="1946843843" data-entity-urn="urn:li:jobPosting:1946843843" data-search-id="e2f62398-982f-463e-9685-9064dfa810eb" data-tracking-id="UDB/ibIoQvCk1l54VneerA==" data-column="1" data-row="1">'''
    soup = BeautifulSoup(html, 'html.parser')
    result = [item['data-id'] for item in soup.find_all('li', attrs={'data-id' : True})]
    print(result)
    Output:-
    ['1946843843']
    

    可能有用beautifulsoap

    【讨论】:

      【解决方案2】:
      # Lets support el is the element
      print(el.attrs['data-id']) # will give you desired answer
      
      

      【讨论】:

        【解决方案3】:
        htm = '<li class="result-card job-result-card result-card--with-hover-state job-card__contents--active" data-id="1946843843" data-entity-urn="urn:li:jobPosting:1946843843" data-search-id="e2f62398-982f-463e-9685-9064dfa810eb" data-tracking-id="UDB/ibIoQvCk1l54VneerA==" data-column="1" data-row="1">'
        
        soup = BeautifulSoup(htm, 'html.parser')
        
        liele = soup.find("li", {"class":"result-card job-result-card"})    
        liDataId = liele['data-id']
        

        【讨论】:

          猜你喜欢
          • 2020-02-13
          • 2014-10-16
          • 1970-01-01
          • 2019-11-07
          • 2018-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-16
          相关资源
          最近更新 更多