【问题标题】:python crawling beautifulsoup how to crawl several pages?python爬取beautifulsoup如何爬取几个页面?
【发布时间】:2026-01-25 20:00:01
【问题描述】:

请帮忙。 我想获取每个页面的所有公司名称,它们有 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)

---------Output of one page

---------page source :)

【问题讨论】:

  • 您已经描述了您的期望,但是您的代码的当前输出是什么?您面临什么问题?而且我认为在发布时您错过了循环的缩进,请更正它以清楚地理解您的代码
  • 在for循环中,获取响应后,您只需从每个页面中选择标签。

标签: python beautifulsoup python-requests web-crawler


【解决方案1】:

那么,您想删除所有headers 并只获取公司名称的string? 基本上,您可以使用soup.findAll 以如下格式查找公司列表:

<strong class="company"><span>중소기업진흥공단</span></strong>

然后你使用.find函数从&lt;span&gt;标签中提取信息:

<span>중소기업진흥공단</span>

之后,您使用.contents 函数从&lt;span&gt; 标签中获取字符串:

'중소기업진흥공단'

因此,您编写一个循环来对每个页面执行相同的操作,并创建一个名为 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 会给你所有你想要的公司名称

【讨论】:

    【解决方案2】:

    我终于明白了。不过还是谢谢你的回答!

    image : code captured in jupyter notebook

    这是我的最终代码。

    from urllib.request import urlopen 
    from bs4 import BeautifulSoup
    
    company_list=[]
    for n in range(12):
        url = 'http://www.saramin.co.kr/zf_user/jobs/company-labs/list/page/{}'.format(n+1)
        webpage = urlopen(url)
        source = BeautifulSoup(webpage,'html.parser',from_encoding='utf-8')
        companys = source.findAll('strong',{'class':'company'})
    
        for company in companys:
        company_list.append(company.get_text().strip().replace('\n','').replace('\t','').replace('\r',''))
    
    file = open('company_name1.txt','w',encoding='utf-8')
    
    for company in company_list:
    file.write(company+'\n')
    
    file.close()
    

    【讨论】:

    • 如果问题解决了,能否请您选择最佳答案?这样人们就知道这个答案已经解决了。