【问题标题】:Scraping from javascript in HTML tags using beautifulsoup使用 beautifulsoup 从 HTML 标签中的 javascript 抓取
【发布时间】:2019-12-04 06:08:38
【问题描述】:

我正在尝试从本网站 http://www.smfederation.org.sg/membership/members-directory 的所有字母(Ato Z 和 0-9)中提取名称

但名字似乎隐藏在href ="javascript:void(0)"

下面是我的代码

import requests 
from bs4 import BeautifulSoup
url = "http://www.smfederation.org.sg/membership/members-directory"
for item in url:
    detail = requests.get(item)
    soup = BeautifulSoup(detail.content, 'html.parser')

我不知道如何在 HTML 中处理 javascript。 我应该在上面的代码中添加什么来获取所有列表的名称?

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup


    【解决方案1】:

    您正在抓取错误的网址。打开浏览器的检查器,转到网络选项卡,您将看到名称为http://smfederation.org.sg/account/getaccounts

    它是json格式,所以当你使用requests返回的响应对象的.json()方法加载它时,它会自动成为一个Python字典:

    >>> import requests
    >>> accounts = requests.get("http://www.smfederation.org.sg/account/getaccounts").json()
    >>> accounts["data"][0]["accountname"]
    'OPTO-PRECISION PTE LTD'
    

    您还可以使用for 循环获取所有帐户,例如:

    for account in accounts["data"]:
        print(account["accountname"])
    

    【讨论】:

    • 这真的很有帮助!!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2018-02-02
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多