【问题标题】:How can I get 'page links' from 'every page'?如何从“每一页”中获取“页面链接”?
【发布时间】:2020-03-14 00:06:17
【问题描述】:

我想通过 python3 从“每一页”中获取“每一页链接”。

在我的代码中,“每一页”的位置位于 BaseUrl 中。而且,在我的代码中,每个页面的链接都位于正文中。

在哪里,

BaseUrl = 'https://www.jobplanet.co.kr/companies?sort_by=review_compensation_cache&industry_id=700&page='

select body = #listCompanies > div > div.section_group > section:nth-child(1) > div > div > dl.content_col2_3.cominfo > dt > a'

请检查我的代码。我想从每个页面收集每个链接,以便将链接列表作为 linkUrl。有什么问题吗?

from bs4 import BeautifulSoup
import csv
import os
import re
import requests
import json

# jobplanet
BaseUrl = 'https://www.jobplanet.co.kr/companies?sort_by=review_compensation_cache&industry_id=700&page='


for i in range(1, 5, 1):
        url = BaseUrl + str(i)
        r = requests.get(url)
        soup = BeautifulSoup(r.text,'lxml')
        body = soup.select('#listCompanies > div > div.section_group > section:nth-child(1) > div > div > dl.content_col2_3.cominfo > dt > a')
        #print(body)

        linkUrl = []
        for item in body:
            link = item.get('href')
            linkUrl.append(link)
print(linkUrl)

【问题讨论】:

    标签: web-scraping beautifulsoup python-requests web-crawler href


    【解决方案1】:

    您选择的 CSS 选择器只返回一条记录。我提供了更简单的 CSS 选择器来返回每页所有 10 条记录。

    你需要在循环外定义列表。

    from bs4 import BeautifulSoup
    import requests
    
    linkUrl = []
    BaseUrl = 'https://www.jobplanet.co.kr/companies?sort_by=review_compensation_cache&industry_id=700&page={}'
    for i in range(1, 6):
        url = BaseUrl.format(i)
        r = requests.get(url)
        soup = BeautifulSoup(r.text,'lxml')
        links=soup.select(".us_titb_l3 >a")
        for item in links:
            link = item.get('href')
            linkUrl.append(link)
    
    print(linkUrl)
    

    【讨论】:

      【解决方案2】:

      您的 Css 选择器错误还添加了分页

      from bs4 import BeautifulSoup
      import csv
      import os
      import re
      import requests
      import json
      from urllib import parse
      
      # jobplanet
      BaseUrl = 'https://www.jobplanet.co.kr/companies?sort_by=review_compensation_cache&industry_id=700&page={}'
      source  =  requests.get(BaseUrl.format(1))
      soup = BeautifulSoup(source.text,'lxml')
      last_page_index = soup.select('a[class="btn_pglast"]') # getting the last page index 
      last_page_index = int(last_page_index[0].get('href').split('page=')[1]) if last_page_index else 1
      for i in range(1, last_page_index):
          print('## Getting Page {} out of {}'.format(i,last_page_index))
          if i > 1: # to avoid getting the same page again
              url = BaseUrl.format(i)
              r = requests.get(url)
              soup = BeautifulSoup(r.text,'lxml')
          body = soup.select('dt[class="us_titb_l3"] a')
          linkUrl = []
          for item in body:
              link = item.get('href')
              link = parse.urljoin(BaseUrl, link)
              linkUrl.append(link)
      print(linkUrl)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 2011-06-04
        • 2014-08-19
        相关资源
        最近更新 更多