【问题标题】:How to get <li> tag information (BeautifulSoup Webscraping)?如何获取<li>标签信息(BeautifulSoup Web Scraping)?
【发布时间】:2020-03-05 04:58:15
【问题描述】:

我正在从这个页面抓取信息:
https://lawyers.justia.com/lawyer/michael-paul-ehline-85006 。 我正在尝试在费用部分下抓取所有信息。 我想要的是以下信息: 免费咨询 是的 接受信用卡 维萨卡、万事达卡、美国运通卡 或有费用 仅在人身伤害案件中。 费率、固定器和其他信息 费率因情况而异。

这是我尝试过的:

for thing in soup.findAll('ul', attrs={"class": "has-no-list-styles"}):
   ul=thing.find('<li>')
   print(ul)

但输出是:

<li>Intellectual Property</li>
<li>Copyright Law</li>
<li><strong>English</strong></li>

提前谢谢你。

更新:我找到了一个解决方案,但它给了我一个无限循环,有什么建议吗?

for o in soup.findAll('div', attrs={"class": "block-wrapper"}):     
    for tag in soup.findAll('div', attrs={"class": "block-wrapper"}):
        if tag.string:
            tag.string.replace_with("")
        for de in o.findAll("li"):
            if de != []:
                de=remove_tags(str(de))
                print (de)

【问题讨论】:

  • "heading-3 block-title iconed-heading font-w-bold" 在这个 div 类中使用 Blocks 类,然后继续使用 thing.find() 作为 span 和 li 标签。
  • 您一次又一次地调用“soup.findAll”两次尝试将第二个“soup.findAll”更改为“o.findAll”
  • @Abe 当我将其更改为 o.findAll 时,它会吐出几乎完全相同的东西

标签: python web beautifulsoup scrape


【解决方案1】:

试试这个。

from simplified_scrapy import SimplifiedDoc,req
html = req.get('https://lawyers.justia.com/lawyer/michael-paul-ehline-85006')
doc = SimplifiedDoc(html)
ul = doc.getElement('ul',attr='class',value='has-no-list-styles',start='class="jicon -large jicon-fee"') # Use class="jicon -large jicon-fee" to locate
print (ul.text)

结果:

Free ConsultationYesCredit Cards AcceptedVisa, Mastercard, American ExpressContingent FeesIn personal injury cases only.Rates, Retainers and Additional InformationRates vary on a case by case basis.

【讨论】:

  • 你知道如何在没有scrapy的情况下做到这一点吗?
  • @Sophia 使用 SimplifiedDoc,您可以使用 start='class="jicon -large jicon-fee"' 进行定位。对于其他库,我没有什么好办法。
【解决方案2】:

试试这汤。它的灵感来自dabinsous的回答。它所做的只是寻找他详述的图标,然后转到其父级的下一个兄弟姐妹,并从那里获取兄弟姐妹的文本。

import requests 
from bs4 import BeautifulSoup 

URL = "https://lawyers.justia.com/lawyer/michael-paul-ehline-85006"
r = requests.get(URL) 
soup = BeautifulSoup(r.content, 'html.parser')
uls = soup.find('span', attrs={"class": "jicon -large jicon-fee"})
print(uls.parent.nextSibling.text)

调整你的抓取来满足这个要求,看看是否有帮助!

【讨论】:

  • 当我这样做时,我只得到:“仅用于初步咨询”
  • 索菲亚,我在使用大兵搜的一些灵感后更新了我的答案。让我知道它是否有效
  • 嗯...这很奇怪,你能解释一下什么不起作用吗?另外,您是复制了整个 sn-p 还是编辑了代码以匹配?
  • 你能看看我的更新吗?我编辑它以匹配
  • 尝试将其复制并粘贴到单独的文件中。另外,你能解释一下什么不起作用吗?
猜你喜欢
  • 2020-04-17
  • 1970-01-01
  • 2021-09-03
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 2012-05-18
相关资源
最近更新 更多