初次小爬蟲:58同城招聘信息爬取


1,通過url獲取html

url="http://bj.58.com/job/pn"+pagenumber+'/?key=python&final=1&jump=1&PGTID=0d000000-0000-046d-babb-93654e2239c8&ClickID=2'
 r=requests.get(url,headers=headers,timeout=30)
 r.raise_for_status()
 r.encoding=r.apparent_encoding

2,headers的改變

因為爬到第二面被擋住了,就加了改了個headers

        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}

3,正則表達式的匹配

根據這段信息得出re表達式

res = re.compile(r'<dl __addition="0".*?<dt><a.*?href="(.*?)".*?> (.*?) </a>.*?<dd.*?> <a.*?>(.*?)</a>.*?</dd>.*?<dd class="w96">(.*?)</dd>.*?<dd class="w68">.*?</dl>',re.S)

4,excel的寫入

   wbk=xlwt.Workbook()
   sheet=wbk.add_sheet('sheet 1') sheet.write(sum, 0,j[0]) sheet.write(sum, 1,j[1]) sheet.write(sum, 2,j[2]) sheet.write(sum, 3, j[3])

5,二級網頁的爬取

因為薪資在另外一個網頁所以加了一個爬取函數,結合了一點bs4庫

        r=requests.get(url)
        r.raise_for_status()
        html=r.text soup=BeautifulSoup(html,"html.parser") temphtml = soup.find_all('div', class_="pos_base_info") res = (r'<span class="pos_salary">(.*?)<') tempre=re.findall(res, str(temphtml))

6,效果圖

 

7,總結

感受:從學python到寫出這個小爬蟲,也花了一個月了,認識到了python的魅力,少了對過程的描述,只需要注重邏輯即可;

收獲:爬蟲讓我對網頁有了個大致的認識,對網頁之間信息的傳遞多了一分理解

8,源碼

因為未知原因已丟失


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM