【问题标题】:unable to Webscrape dropdown item [Python][beautifulsoup]无法 Webscrape 下拉项 [Python][beautifulsoup]
【发布时间】:2020-08-05 07:12:31
【问题描述】:

我是网络抓取的新手,我正在抓取一个网站 - https://www.valueresearchonline.com/funds/22/uti-mastershare-fund-regular-plan/

在此,我想刮掉这段文字 - 常规计划

但问题是,当我使用检查元素执行此操作时, 代码 -

import requests
from bs4 import BeautifulSoup
import csv
import sys

url = 'https://www.valueresearchonline.com/funds/newsnapshot.asp?schemecode=22'
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
regular_direct = soup.find('span',class_="filter-option pull-left").text

print(regular_direct)

我在打印中没有得到任何结果,我不知道为什么,inspect element 和 view page source 中的代码也不同,因为在 view page source 中,这个 span 和 class 不存在。 为什么我什么都没有??谁能告诉我,我怎样才能得到那个文本以及为什么检查元素代码和查看页面源代码不同?

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    你需要更改选择器,因为下载的 html 源不同。

    import requests
    from bs4 import BeautifulSoup
    import csv
    import sys
    
    url = 'https://www.valueresearchonline.com/funds/newsnapshot.asp?schemecode=22'
    res = requests.get(url)
    soup = BeautifulSoup(res.text, "html.parser")
    regular_direct = soup.find("select", {"id":"select-plan"}).find("option",{"selected":"selected"}).get_text(strip=True)
    
    print(regular_direct)
    

    输出:

    Regular plan
    

    【讨论】:

    • 是的,它正在被抓取,但你能告诉我应该使用检查元素还是使用查看源代码来抓取?你是怎么得到那个选择器的,我的意思是特定的选择器?或者你怎么知道你应该使用这个?
    • Inspect 元素在浏览器上,不能保证使用requests 发送相同的html。因此,最好的选择是打开下载的 html。使用requests,然后使用检查元素
    • 好的,非常感谢,我从昨天开始就卡在这里了,
    • @Prabhatkumar 为了澄清原因,requests 不执行 Javascript 而您的浏览器执行。为了缓解这种情况,您可以使用requests-html,它执行 Javascript 并具有与requests 类似的 API。 requests.readthedocs.io/projects/requests-html/en/latest
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2021-09-29
    • 2020-02-06
    相关资源
    最近更新 更多