【问题标题】:Unable to scrape class information with Find_All无法使用 Find_All 刮取班级信息
【发布时间】:2021-04-26 05:30:27
【问题描述】:

我正在尝试从网站https://www.programmableweb.com/category/all/apis 下方提取班级信息表格。我的代码适用于除https://www.programmableweb.com/category/all/apis?page=2092 之外的所有页面。

from bs4 import BeautifulSoup
import requests

url = 'https://www.programmableweb.com/category/all/apis?page=2092'
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
apis = soup.find_all('tr',{'class':['odd views-row-first', 'odd','even','even views-row-last']})
print(apis)

在 2092 页面上,我仅获得以下 1 个班级的信息

[<tr class="odd views-row-first views-row-last"><td class="views-field views-field-pw-version-title"> <a href="/api/inkling">Inkling API</a><br/></td><td class="views-field views-field-search-api-excerpt views-field-field-api-description hidden-xs visible-md visible-sm col-md-8"> Our REST API allows you to replicate much of the functionality in our hosted marketplace solution to build custom widgets and stock tickers for your Intranet, create custom reports, add trading...</td><td class="views-field views-field-field-article-primary-category"> <a href="/category/financial">Financial</a></td><td class="views-field views-field-pw-version-links"> <a href="/api/inkling-rest-api">REST v0.0</a></td></tr>]

对于任何其他页面(如https://www.programmableweb.com/category/all/apis?page=2091),我会获得有关所有课程的信息。 HTML 结构在所有页面中似乎都相似。

【问题讨论】:

  • 它对我来说工作正常,也许尝试在每个请求之间睡几秒钟
  • @AhmedSoliman,你能粘贴你得到的打印输出吗?我是这里的新手,您也可以帮忙解决您建议的睡眠代码

标签: html web-scraping beautifulsoup


【解决方案1】:

这个网站不断地向它的数据库添加新的 API,所以这里有三种情况可能会导致这种情况:

  1. 您使用的选择器不准确。
  2. 网站有 为您发送过多请求采取某种安全措施。
  3. 在 你刮这个页面的时候确实有一个项目。

场景 3 最有可能相信。

from bs4 import BeautifulSoup
import requests
from time import sleep

for page in range(1,2094): #starting with 1 then the last page will be 2093
  url = f'https://www.programmableweb.com/category/all/apis?page={page}'
  response = requests.get(url)
  data = response.text
  soup = BeautifulSoup(data, 'html.parser')
  apis = soup.select('table[class="views-table cols-4 table"] tbody tr') # better selector
  print(apis) #page 2093 currently has 6 items on it .
  sleep(5) #This will sleep for 5 secs 

【讨论】:

  • 场景 3 是什么意思?页面 (programmableweb.com/category/all/apis?page=2092) 直观地显示了 6 个 API,但代码仅提取了 1 个 API。我只在上面的页面上尝试,不知道出了什么问题。即使使用您的选择器,我也会获得 1 个 API
  • @Learner 我的意思是您的代码本来可以正常工作,但是在抓取此页面时,它确实只有一个项目,这不是您认为的问题。
猜你喜欢
  • 2011-02-26
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多