鱼C论坛

 找回密码
 立即注册
查看: 1766|回复: 7

[已解决]爬取58同城

[复制链接]
发表于 2018-2-24 18:42:04 | 显示全部楼层 |阅读模式
5鱼币
问题,知道字典中字典的键,怎么取它的值,不知道外层字典键的情况下
  1. import requests
  2. import bs4,pickle

  3. def open_url(url):
  4.     headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'}
  5.     res=requests.get(url,headers=headers)
  6.     soup=bs4.BeautifulSoup(res.text,'lxml')
  7.     return soup

  8. def getxinxi(url):
  9.     soup=open_url(url)
  10.     #获取招聘信息
  11.     tager1=soup.find_all('li',class_='job_item clearfix')
  12.     for each in tager1:
  13.         xinxi=""
  14.         for each2 in each.div.div.a.strings:
  15.             xinxi+=each2.strip()
  16.         print('%-25s %10s %25s'%(xinxi.strip(),each.div.p.text,each.div.next_sibling.div.a.text.strip()))
  17. def getcity(dict1,city):#取城市在字典中的值
  18.     dict2=dict1.items()
  19.     for each in dict2:
  20.         for each2 in each:

  21.             if type(each2)==dict():
  22.                 getcity(each2)
  23.             else:
  24.                 if city in each2:
  25.                     return each2
  26. def main():
  27.     city=input('请输入要查询的城市:')
  28.     list1=open('my_citylist.pkl','rb')
  29.     citylist=pickle.load(list1)
  30.     diqu=getcity(citylist,city)[city].split('|')[0]#获取地区代码
  31.     #地区是字典,字典里面有字典,怎么查询,我这个有问题,有帮助
  32.     url='http://'+diqu+'.58.com'
  33.     soup=open_url(url)
  34.     zhiwei=input('请输入你要查询的职位:')
  35.     tager=soup.find_all('div',class_='col4')
  36.     tager2=tager[1].find_all('a')
  37.     for each in tager2:
  38.         if each.string==zhiwei:
  39.             zhiwei=url+each['href']
  40.     soup=open_url(zhiwei)
  41.     tager=soup.find('i',class_='total_page')
  42.     yeshu=int(tager.string)
  43.     cont=1   
  44.     while 1:
  45.         print('一共%d页当前第%d页'%(yeshu,cont))
  46.         url='http://hshi.58.com/pugong/pn'+str(cont)+'/'
  47.         getxinxi(url)
  48.         page=input('按q上一页,按p下一页')
  49.         if page=='p':
  50.             if cont==yeshu:
  51.                 print('已经是最后一页,没有下一页了')
  52.             else:
  53.                 cont+=1
  54.         elif page=='q':
  55.             if cont==1:
  56.                 print('已经是第一页,没有上一页了')
  57.             else:
  58.                 cont-=1
  59. if __name__=="__main__":
  60.     main()
复制代码

程序是OK的,就那个问题没处理掉
最佳答案
2018-2-24 18:42:05
小武design 发表于 2018-5-31 16:49
Traceback (most recent call last):
  File "C:/Users/昊/Desktop/58.py", line 1, in
    import requ ...

pip install requests

城市包.zip

9.22 KB, 下载次数: 7

最佳答案

查看完整内容

pip install requests
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-24 18:42:05 | 显示全部楼层    本楼为最佳答案   
小武design 发表于 2018-5-31 16:49
Traceback (most recent call last):
  File "C:/Users/昊/Desktop/58.py", line 1, in
    import requ ...

pip install requests
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-24 19:03:31 | 显示全部楼层
只能输入城市
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-24 22:55:29 From FishC Mobile | 显示全部楼层
#coding: utf-8
import types

#获取字典中的objkey对应的值,适用于字典嵌套
#dict:字典
#objkey:目标key
#default:找不到时返回的默认值
def dict_get(dict, objkey, default):
    tmp = dict
    for k,v in tmp.items():
        if k == objkey:
            return v
        else:
            if type(v) is types.DictType:
                ret = dict_get(v, objkey, default)
                if ret is not default:
                    return ret
    return default

#如
dicttest={"result":{"code":"110002","msg":"设备设备序列号或验证码错误"}}
ret=dict_get(dicttest, 'msg', None)
print(ret)
自己找到方法了-_-
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-24 22:57:10 From FishC Mobile | 显示全部楼层
要鱼币的来吧,方法找到了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-31 16:49:54 | 显示全部楼层
Traceback (most recent call last):
  File "C:/Users/昊/Desktop/58.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-31 16:51:24 | 显示全部楼层
我提示的这个是什么意思啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-1 20:55:39 | 显示全部楼层

不太懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 14:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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