python實(shí)戰(zhàn)計(jì)劃第二周第二次:電話號(hào)碼

處理詳情頁面的字符串費(fèi)了些勁,其他還好。

我的成果

屏幕快照 2016-09-01 下午9.23.04.jpg

我的代碼

from bs4 import BeautifulSoup
import requests
import time
import pymongo

client=pymongo.MongoClient('localhost',27017)
phone_num=client['phone_num']
sheet_num=phone_num['sheet_num']
sheet_info=phone_num['sheet_info']

def phone_links(num):
    url='http://bj.58.com/shoujihao/pn{}/?'.format(num)
    time.sleep(1)
    wb_data=requests.get(url)
    soup=BeautifulSoup(wb_data.text,'lxml')
    titles=soup.select('a.t > strong')
    phonenum_links=soup.select('#infolist > div > ul > div > ul > li > a.t')
    if soup.select('#infocont > span > b')[0].text=='0':
        return
    else:
        for title,phonenum_link in zip(titles,phonenum_links):
            data={
                'title':title.get_text(),
                'phonenum_links':phonenum_link.get('href').split('?')[0]
            }
            sheet_num.insert_one(data)
            print(data)

def get_num_info(url):
    time.sleep(1)
    wb_data=requests.get(url)
    soup=BeautifulSoup(wb_data.text,'lxml')
    title=soup.select('div.col_sub.mainTitle > h1')[0]
    price=soup.select('div.su_con > span')[0]
    data={
        'url':url,
        'title':title.get_text().replace("\n","").replace(" ","").replace("\t","")[:11],
        'price':price.get_text().replace("\n","").replace(" ","").replace("\t","")
    }
    sheet_info.insert_one(data)
    print(data)

for i in range(1,1000):
    phone_links(i)

for info in sheet_info.find():
    get_num_info(info['url'])

總結(jié)

  • 網(wǎng)頁各有不同,需要根據(jù)具體的特點(diǎn)分析自己需要爬取的數(shù)據(jù),應(yīng)該采用什么方法才能有效的獲取。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容