【问题标题】:Scraping data using span title and span class使用跨度标题和跨度类抓取数据
【发布时间】:2018-07-17 14:56:59
【问题描述】:

我正在使用 Python Anaconda 将数据抓取到 Excel 工作表中。我在两个网站上遇到了一些问题。

网站 1

<div id="ember3815" class="ember-view">
<p class="org-top-card-module__company-descriptions Sans-15px-black-55%">
<span class="company-industries org-top-card-module__dot-separated-list">
  Industry
</span>
<span class="org-top-card-module__location org-top-card-module__dot-separated-list">
  City, State
</span>
<span title="62,346 followers" class="org-top-card-module__followers-count org-top-card-module__dot-separated-list">
  62,346 followers
</span>

我正在尝试提取跨度标题。我尝试过的东西(我也尝试过所有作为 find_all 的东西):

text = soup.find('span',{'class':"company-industries org-top-card-module__dot-separated-list"})

text = soup.find('p',{'class':"org-top-card-module__company-descriptions Sans-15px-black-55%"})

text = soup.body.find('span', attrs={'class': 'org-top-card-module__location org-top-card-module__dot-separated-list'})

text = soup.find('span',{'class': 'org-top-card-module__location org-top-card-module__dot-separated-list'})

我确定我还尝试过其他一些我没有列出的东西,因为我不记得所有的东西。我不是程序员,我只是想弄清楚这一点以提取数据进行分析。帮助?

站点 2

我需要从下面的 html 中提取值 8,052。

<section class="zwlfE">
<div class="nZSzR">...</div>
<ul class="k9GMp ">
<li class="Y8-fY ">...</li>
<li class-"Y8-fY ">
<a class="g47SY " title="8,052">8,052</span>" followers"
</a>
</li>
<li class="Y8-fY ">...</li>
</ul>
<div class="-vDIg">...</div>
</section>

我试过了:

  • text = soup.find('span',{'class': "g47SY "})
  • 与上面类似,但带有 div 和 li 标签

我尝试过的所有结果都是 []。

请帮忙?

【问题讨论】:

    标签: python web-scraping beautifulsoup anaconda


    【解决方案1】:

    获取span title

    from bs4 import BeautifulSoup
    html ="""<div id="ember3815" class="ember-view">
    <p class="org-top-card-module__company-descriptions Sans-15px-black-55%">
    <span class="company-industries org-top-card-module__dot-separated-list">
      Industry
    </span>
    <span class="org-top-card-module__location org-top-card-module__dot-separated-list">
      City, State
    </span>
    <span title="62,346 followers" class="org-top-card-module__followers-count org-top-card-module__dot-separated-list">
      62,346 followers
    </span>"""
    
    soup = BeautifulSoup(html, "html.parser")
    print( soup.find("span", class_="org-top-card-module__followers-count org-top-card-module__dot-separated-list")["title"])
    

    输出:

    62,346 followers
    

    对于site2

    print( soup.find("a", class_="g47SY")["title"])
    

    【讨论】:

    • 导入请求 call = requests.get('site') page = call.text from bs4 import BeautifulSoup soup(page, 'lxml') print(soup.find("span", class_ ="org-top-card-module__followers-count org-top-card-module__dot-separated-list")["title"])
    • 我得到的结果是“TypeError: 'Nonetype' object is not subscriptable”
    • 我在尝试上面建议的 print(soup...) 时都遇到同样的错误。
    • 检查你的内容是否在call.text 有时可以通过JS加载数据
    • 我打印了 call.text 并且我看到了那里的内容(但内容太多以至于很难判断是否有特定的内容)。什么是JS? Javascript?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2020-08-02
    相关资源
    最近更新 更多