鱼C论坛

 找回密码
 立即注册
查看: 1262|回复: 3

爬虫代码有问题请各位大佬指点

[复制链接]
匿名鱼油
匿名鱼油  发表于 2021-12-19 13:54:36 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 匿名 于 2021-12-19 17:30 编辑

我爬取的网站是https://www.shixi.com/search/index?key=大数据,内容是全部页数的六个信息
这个代码我是照着爬取豆瓣TOP250的那个代码写的,希望有人指点我一下

  1. import requests
  2. import bs4
  3. import re

  4. def open_url(url):
  5.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
  6.     res = requests.get(url, headers=headers,verify=False)
  7.     return res

  8. def find_occupation(res):
  9.     soup =bs4.BeautifulSoup(res.text, 'html.parser')

  10.     #岗位名称
  11.     post = []
  12.     targets = soup.find_all("dt", class_="job-name")
  13.     for each in targets:
  14.         post.append(each.text)

  15.     #公司名称
  16.     company = []
  17.     targets = soup.find_all("div", class_="comany-info-title")
  18.     for each in targets:
  19.         company.append(each.a.text)

  20.     #工作地点
  21.     area = []
  22.     targets = soup.find_all("span", class_="job-address")
  23.     for each in targets:
  24.         area.append(each.text)


  25.     #学历要求
  26.     education = []
  27.     targets = soup.find_all("dd", class_="job-des")
  28.     for each in targets:
  29.         education.append(each.text)

  30.     #薪资情况
  31.     pay = []
  32.     targets = soup.find_all("div", class_="comany-info-des")
  33.     for each in targets:
  34.         pay.append(each.text)
  35.    
  36.     #发布时间
  37.     time = []
  38.     targets = soup.find_all("span", class_="job-time")
  39.     for each in targets:
  40.         try:
  41.             time.append(each.text.split('\n')[1].strip() + each.text.split('\n')[2].strip())
  42.         except:
  43.             continue

  44.     result = []
  45.     length = len(post)
  46.     for i in range(length):
  47.         result.append(post[i] + company[i] + area[i] + education[i] + pay[i] + time[i] + '\n')
  48.     return result

  49. #找出共有多少个页面
  50. def find_depth(res):
  51.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  52.     depth = soup.find('li', class_='next').previous_sibling.previous_sibling.text

  53.     return int(depth)

  54. def main():
  55.     host = "https://www.shixi.com/search/index?key=大数据"
  56.     res = open_url(host)
  57.     depth = find_depth(res)

  58.     result = []
  59.     for i in range(depth):
  60.         url = host + '&page=' + str(10 * i)
  61.         res = open_url(url)
  62.         result.extend(find_occupation(res))

  63.     with open ("大数据.txt", "w", encoding="utf-8") as f:
  64.         for each in result:
  65.             f.write(each)


  66. if __name__ == "__main__":
  67.     main()   
复制代码
回复

使用道具 举报

发表于 2021-12-19 20:46:06 | 显示全部楼层
顶一下,有没有大佬帮忙解答一下啊,孩子球球了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-19 15:06:18 | 显示全部楼层
suchocolate 发表于 2021-12-19 14:49
find_movies函数是做什么 ?

是我忽略了没改,应给是 find_occuptation(res)
虽然还未能运行,不过谢谢指出
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-19 14:49:49 | 显示全部楼层
find_movies函数是做什么 ?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-11 11:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表