繁体   English   中英

Python-Beautiful Soup刮板返回一些(但不是全部)文本

[英]Python - Beautiful Soup scraper returning some, but not all, text

我正试图从这份名单中剔除美国前100名工作。 当我运行此代码时:

import urllib.request
from bs4 import BeautifulSoup
url = 'https://www.ranker.com/list/most-common-jobs-in-america/american-jobs'
page_opened = urllib.request.urlopen(url)

soup = BeautifulSoup(page_opened, 'html.parser')
jobs_soup = soup.find_all('span','listItem__title')
print(jobs_soup)

“美丽的汤”返回的是我期望的,带有标签的职位,唯一的例外是“中学教师”,该职位在100个职位中仅排名25。 我在其他网页上以同样的方式使用了Beautiful Soup,没有问题。 网页/我的代码是否有些时髦,导致输出不完整?

在浏览器的开发人员工具中打开“网络”选项卡后,我看到在滚动时发出了XHR请求,并且某些响应包含列表项。 您仅能获得前24个项目,因为没有触发这些请求。 其中一个请求的网址是:

https://cache-api.ranker.com/lists/354954/items?limit=20&offset=50&include=votes,wikiText,rankings,openListItemContributors&propertyFetchType=ALL&liCacheKey=null

通过将限制更改为100,将偏移量更改为0,我获得了前100个工作:

import json
from urllib.request import urlopen

# I removed the other query parameters and it still seems to work
url = 'https://cache-api.ranker.com/lists/354954/items?limit=100&offset=0'
resp = urlopen(url)
data = json.loads(resp.read())
job_titles = [item['name'] for item in data['listItems']]
print(len(job_titles))
print([job_titles[0], job_titles[-1]])

输出:

100
['Retail salespersons', 'Cleaners of vehicles and equipment']

暂无
暂无

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

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