python - python爬取beautifulsoup如何爬取几个页面?
问题描述
请帮忙。我想获取每个页面的所有公司名称,它们有 12 页。
http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/1 http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/2 -- 本网站只更改号码。
所以这是我到目前为止的代码。我可以只得到 12 页的标题(公司名称)吗?先感谢您。
from bs4 import BeautifulSoup
import requests
maximum = 0
page = 1
URL = 'http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/1'
response = requests.get(URL)
source = response.text
soup = BeautifulSoup(source, 'html.parser')
whole_source = ""
for page_number in range(1, maximum+1):
URL = 'http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/' + str(page_number)
response = requests.get(URL)
whole_source = whole_source + response.text
soup = BeautifulSoup(whole_source, 'html.parser')
find_company = soup.select("#content > div.wrap_analysis_data > div.public_con_box.public_list_wrap > ul > li:nth-child(13) > div > strong")
for company in find_company:
print(company.text)
解决方案
那么,您想删除所有headers
并仅获取string
公司名称吗?基本上,您可以使用soup.findAll
以下格式查找公司列表:
<strong class="company"><span>중소기업진흥공단</span></strong>
然后使用该.find
函数从<span>
标签中提取信息:
<span>중소기업진흥공단</span>
之后,您使用.contents
函数从<span>
标签中获取字符串:
'중소기업진흥공단'
因此,您编写一个循环来对每个页面执行相同的操作,并创建一个调用列表company_list
来存储每个页面的结果并将它们附加在一起。
这是代码:
from bs4 import BeautifulSoup
import requests
maximum = 12
company_list = [] # List for result storing
for page_number in range(1, maximum+1):
URL = 'http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/{}'.format(page_number)
response = requests.get(URL)
print(page_number)
whole_source = response.text
soup = BeautifulSoup(whole_source, 'html.parser')
for entry in soup.findAll('strong', attrs={'class': 'company'}): # Finding all company names in the page
company_list.append(entry.find('span').contents[0]) # Extracting name from the result
company_list
将为您提供您想要的所有公司名称
推荐阅读
- python-3.x - 实施 celery 搞砸了我的 INSTALLED_APPS (ModuleNotFoundError: No module named myapp)
- html - HTML中选项后面的提交按钮
- nginx - nginx 在同一台服务器上缓存几个上游
- python - Django:如何处理计算密集型模型
- python-3.x - 如何更改 sklearn.metrics 包中的 plot_confusion_matrix 默认图形大小
- python - 抓取 worldometers 主页以提取 COVID-19 表数据,但未正确提取值(Python)
- postgresql - 如何在 Spring Boot 和 Postgres 中将 XY 和 SRID 4326 插入几何?
- c++ - 尝试使用模板化函数交换两个字符串
- c# - 即使 Exif 信息可用,暴露日期也返回为 null
- docker - 如何删除在 docker 中创建的 chromedriver 会话?