繁体   English   中英

打印“find_all”的结果(使用 bs4 库)返回一个空列表(但我引用的类存在)

[英]Printing the result of a "find_all" (using bs4 library) returns an empty list (but the class I'm referencing exists)

我正在尝试为https://www.cappex.com/scholarships创建一个 webscraper。 我试图找到每个包含“ais-hits--item”类的奖学金信息的div。 使用 find_all (来自 bs4)时,我正在寻找的 div 没有被返回,我很困惑为什么。 我对python相当陌生,但对HTML不是。 在此处输入图片说明 有很多 div 相互嵌套,所以我尝试找到其他具有不同类的 div,它们都返回一个空列表 ([])。 我在做一些根本错误的事情吗?

import requests
from bs4 import BeautifulSoup


url = 'https://www.cappex.com/scholarships'
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')
scholarships = soup.find_all('div', class_='ais-hits--item')

print scholarships

我希望有一个 div 列表,但输出是 []。

事实证明<div>标签没有与页面源一起加载,因此无法被BeautifulSoup捕获。 换句话说,网站加载后可能会触发一个事件,因此bs4不会帮助您获取数据。 您可以通过搜索标签ais-hits--item的页面来源进行验证。

话虽如此,您实际上可以直接查询您发布的特定网站中的数据。 请记住,当您选择这样做时,该网站是否希望您拥有该访问权限。

headers = {
    'accept': 'application/json',
    'content-type': 'application/x-www-form-urlencoded',
    'Origin': 'https://www.cappex.com',
    'Referer': 'https://www.cappex.com/scholarships',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}

json = {"requests":[{"indexName":"prod-scholarship","params":"query=&hitsPerPage=12&maxValuesPerFacet=10&page=0&attributesToRetrieve=%5B%22name%22%2C%22administeringAgency%22%2C%22deadline%22%2C%22deadlineFormatted%22%2C%22awardAmount%22%2C%22maxAward%22%2C%22averageAwardAmount%22%2C%22variableAwardAmount%22%2C%22renewable%22%2C%22objectID%22%5D&restrictHighlightAndSnippetArrays=true&facets=%5B%22deadline%22%2C%22awardAmount%22%2C%22renewable%22%2C%22firstGeneration%22%2C%22financialNeedRequired%22%2C%22lgbtqia%22%2C%22disability%22%2C%22nonUSCitizenEligible%22%2C%22genders%22%2C%22ethnicities%22%2C%22enrollmentLevels%22%5D&tagFilters="}]}

params = {
    'x-algolia-agent': 'Algolia for vanilla JavaScript 3.27.1;instantsearch.js 2.8.0;JS Helper 2.26.0',
    'x-algolia-application-id': 'MVAUKZTA2I',
    'x-algolia-api-key': 'd9568940e07ac01d868893e44be784e8'
}

url = 'https://mvaukzta2i-dsn.algolia.net/1/indexes/*/queries'
r = requests.post(url, headers=headers, params=params, json=json)

这将获得网站的所有数据。 例如:

results = r.json()['results']

results[0]['hits'][0]
Out[1]:
{'administeringAgency': 'My Best Mattress',
 'renewable': False,
 'name': 'MyBestMattress Scholarship',
 'deadlineFormatted': 'July 31, 2020',
 'awardAmount': 700.0,
 'averageAwardAmount': 700.0,
 'deadline': 1596153600000.0,
 'variableAwardAmount': False,
 'objectID': '52049',
 '_highlightResult': {'administeringAgency': {'value': 'My Best Mattress',
   'matchLevel': 'none',
   'matchedWords': []},
  'name': {'value': 'MyBestMattress Scholarship',
   'matchLevel': 'none',
   'matchedWords': []},
  'deadlineFormatted': {'value': 'July 31, 2020',
   'matchLevel': 'none',
   'matchedWords': []},
  'awardAmount': {'value': '700.0', 'matchLevel': 'none', 'matchedWords': []},
  'id': {'value': '52049', 'matchLevel': 'none', 'matchedWords': []},
  'averageAwardAmount': {'value': '700.0',
   'matchLevel': 'none',
   'matchedWords': []},
  'deadline': {'value': '1.5961536E+12',
   'matchLevel': 'none',
   'matchedWords': []}}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM