Python爬虫实战--(四)爬取58同城的商品数据

  • 获取商品信息
    • 获取头文件中的title信息
    • 获取价格信息
    • 获得时间信息
  • 从列表页获取url
  • 完整代码

获取商品信息

Python爬虫实战--(四)爬取58同城的商品数据_第1张图片

获取头文件中的title信息

<title>【图】(上门回收)苹果平板-笔记本-微软平板全系列IPad-Pro-mini4-Air2 - 平板电脑 - 北京58同城</title>
title = soup.title.text
print(title)
【图】(上门回收)苹果平板-笔记本-微软平板全系列IPad-Pro-mini4-Air2 - 平板电脑 - 北京58同城

获取价格信息

<span class="price c_f50">5000span>

select()返回的是一个列表,如果我们要爬的内容只有一处的话那么返回的列表的长度为1。所以我们直接用索引0进行访问就可以了。

price = soup.select('span.price.c_f50')[0].get_text()
print(price)
5000

获得时间信息

<li class="time" title="发布日期">2016-05-19li>
time = soup.select('li.time')[0].get_text()
print(time)
2016-05-19

从列表页获取url

写一个函数从列表页面中获得每一个产品的URL。原理和之前相同此处不再赘述。
Python爬虫实战--(四)爬取58同城的商品数据_第2张图片

<a class="title t" href="http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqkpyOMmh78uA-6UhO6UztzPWTvnWm3nHEOnW03ng980v6YUykKnH9dPHTLnjNOnHb3P1bLnjDvnjTkP1b3rj9zTHczrHbOn1c3njT3PjNdTEDzPWTvnWm3nHEOnW03nEDzPWmKnH0kTHc1njbYTHDKnHNQPW0knj9vPW01rTDQTyQG0Lw_uyuYTHDKnE7-0MPCULRYudtznjDLnjbkP7q-XZKtnEDVnEDKnTDkTEDQTHP6mHK-mHbLsHmkPhmVPADzuBYOPj91symkPh7hrjE3uHEvnTD1PjEYPjEQPEDznWcvPjnYPjc1rHmYPj9KTHc1njbYTHDKna3znWnLnHNQPHTOrjm3nWN1n9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQsk7WPiq8p-ukUH-vNLKwPMwZEg6HnD7M5HYKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exE7WuWm3n1EknWnQPHDzmHEQ&psid=185507059198797016000798882&entinfo=26062681492781_0" target="_blank" rel="nofollow">(上门回收)苹果平板-笔记本-微软平板全系列Ia>
def get_links_from():
    urls = []
    list_view = 'http://bj.58.com/pbdn/1/'
    wb_data = requests.get(list_view)
    soup = BeautifulSoup(wb_data.text,'lxml')
    for link in soup.select('a.title.t'):
        urls.append(link.get('href'))
    return urls
print(get_links_from())
['http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqkpyOMmh78uA-6UhO6UztzPWTvnWm3nHEOnW03ng980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THczrHbOn1c3njT3PjNdTEDzPWTvnWm3nHEOnW03nEDzPWmKnWD3THc1njbYTHDKnHNQPW0knjbdnH01P9DQTyQG0Lw_uyuYTHDKnE7-0MPCULRYudtznjDLnjbkP7q-XZKtnEDVnEDKnTDkTEDQTHIbPAcLuAPbsyNvrHDVPj0LmzdBnyDOsyubuHT1PhF-uhcOuTD1PjEYPjEQPEDznWcvPjnYPjc1rHmYPj9KTHc1njbYTHDKna3QrjnzP1bknW9LPHDvPHEkP9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exE76ujTLnAPWnWcknW9kPjTL&psid=134070767198797027142256658&entinfo=26062681492781_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqkpyOMmh78uA-6UhO6Uzt1nWEzPWEYn1bzPjcLnZ980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THNkrHnkPHbYnjbdnHDLTED1nWEzPWEYn1bzPjcLnTDdnTDdnTDzn1TOPTDQTHDdnHmLnjTOPHDLn1mKn97_pgPYUARhITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnE76uW7huHRbmiYOrjuBsHEOPyDVryNLPzYdmyndmWNLnAD3PHbKn1NYnWNkPHEKnWnkrjTdnHTOrjb3Pj9kTEDzn1TOPTDQTHT8PWn3n1Dzn1E3rjmkPWcOn9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exEDzujnzmHw-PHNduARhuWEv&psid=134070767198797027142256658&entinfo=32426443924270_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvwMsWN3shPfUiqkpyOMmh78uA-6UhO6Uzt1nW9knjbvnWnLrjNYPL980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THNzn1NzPjcOPHmOPHN3TED1nW9knjbvnWnLrjNYPkD1nTD1nTDzn1TOPTDQTHDdnHmLnjTOPHDLn10Knk7_pgPYUARhITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnE7hPvn1PAcduiYOmv76sHEOP1DVmWEdraYLnHb1nvPhuWPhmH9Kn1NLPjmQn1cKnWnYnHEYrjNdPWEQn1c3TEDzn1TOPTDQTHD8njNkP1T3P10YPjNzrjcvPkDKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exE7-myPhrHwBuHK6mWF-m1DY&psid=134070767198797027142256658&entinfo=32800962378547_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsv71sWN3shPfUiqkpyOMmh78uA-6UhO6Uzt1nW0kPH01rH03n1NknM980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THNknHNYP10dPHTOnWmdTED1nW0kPH01rH03n1Nkn9DzPTDzPTDzn1TOPTDQTHDdnHmLnjTOPHDLn10KPT7_pgPYUARhITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnEDzPymYnH9QnadhmvuWsHwWPjcVrHP-uBYknjnkmH01uH0LPHcKn1NLPj91nH0KnWnYnHmkP1DOrjEzPHEYTEDzn1TOPTDQTHD8n1TvrH9YrjmYrjn1n1NYn9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exED1PhFbPWn1mH6hnHD3nHbd&psid=134070767198797027142256658&entinfo=32705739783502_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsLPLsWN3shPfUiqkpyOMmh78uA-6UhO6Uzt1nWDOnWnQrjnkPH0QPg980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THn3PW03PWb1P10OP1cvTED1nWDOnWnQrjnkPH0QPEDzrEDzrEDzn1TOPTDQTHDdnHmLnjTOPHDLn10KPE7_pgPYUARhITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnED3m191m16bPBd-nHmzsHE1PWcVmH6WniYOnWPhPvR-nhwhrAnKn1NLPjEQn19KnWnYnHnvP1TdnHD1n1EYTEDzn1TOPTDQTHD8njm1PWT3nHT3PjmOPHTYP9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exE76uj6hm19zmvE1PjcLPynQ&psid=134070767198797027142256658&entinfo=32192318305715_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqkpyOMmh78uA-6UhO6UztzPWTknHb3PjnQrHEznM980v6YUykKnHnYnj0kP1mLnHb3P1bLnjcLnHEznWNvPWN3THEkrjnYrjTkTEDzPWTknHb3PjnQrHEzn9D1P9D1P9Dzn1TOPTDQTHDdnHmLnjTOPHDLn10KP97_pgPYUARhITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnEDQuj7bmWEQPaY1ryEdsHEzPjDVrAuhPaYdnHPWmhcLmH6BmvDKn1ELn1cQnHEKnWcdnHNdPj01rHmLn1NzTEDzn1TOPTDQTHT8rjNQrHm3nHDQnWEdPW9QrTDKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTED1rjE3PT7exEDYmyDLnAckmW6BuH91myw-&psid=134070767198797027142256658&entinfo=26001984319422_0', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1cdPWmdnWTQrjmzrH0zXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KnHb1PW9YP1cvnjbkn1DKTHcdPWmdnWTQrjmzrH0zTHbznTDOnWTKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTHDKugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKnW7-PhEvnvcVPHTdPiYYuyRbsH9vm10VuHDQm1FBm1EYrjKhTHcYP193rHD1THDLrj0vP1mkPjNOn1bYPTDKrHDzPEDQTHT8n1m1PHc3njcdnW0dnWEznkDKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTEDQnjnKXLYKmHT3nvPWuAckm1DkrAN3PE&entinfo=25665201862972_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1nQn19vPHNLPjbLnW0kXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KnWNkPj0OnjcdPHc3n1bKTHnQn19vPHNLPjbLnW0kTHDknjTKnHTknTDzn1TOPTDQTHDdnHmLnjTOPHDLn1NKn97-0dqkmdqC0dqzpyICITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnEDzujTknWDdridbnvDksHE1P1DVmyw6uBdBPj6hmy7brju-rHnKn1EdPHEkn1mKnWc1PjcOrjNkP1mOPW0zTEDOnHcdTHDKna31n1T1PWndnWbdP1bYnWmvTEDVTEDKpZwY0jCfsvFJsWN3shPfUiqkmhw8s1DfTEDQnHE8nWELsWNvsWDLPTDkTHTKTHDknk7exEDdryD1uhEOn1P6P1N1uAwb&entinfo=31386557497270_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1czrjcvnWDYnWbOrHD1XaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9Kn1cOrHDLP1nOn1mOnj9KTHczrjcvnWDYnWbOrHD1THmknTDvnjTKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTHnKugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKPHIBuWm1nWbVPHEQuBYYmvRBsH9LmyEVPA7bPWcLm1KBPj-bTHDYnWbkn1TkTHDdPjbdPHb1PjNkn1TvPTDKrHDzPEDQTHT8PHEOrjc3Pj0dn1TvnHmvTEDVTEDKpZwY0jCfsvFJsWN3shPfUiqkmhw8s1DfTEDQnHE8nWELsWNvsWDLPTDkTHTKTHDknk7exEDzPjwWnvndnjbQuHnQPW9v&entinfo=22826214299913_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1nznH91Pj9OPj0zPjN3XaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KnH0knjTOnWDOnHN1rH9KTHnznH91Pj9OPj0zPjN3THbdnTDOPHTKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTHEKugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKnHmLPyEvPH9VujP-PiYYP1bYsyDdnjcVujNQPjEznAEYuAEzTHndnWDOPjbvTHczrHT1Pjmdnj0QPjEknTDKrHDzPEDQTHT8n1EvP1nYPHcznWbznjTknWNKTiYKTE7CIZwkrBtfmhC8PH98mvqVsLKBuA3fnitKTHDQPa3zPj08PHm8nH0YTHTKnTDKnHT1TgVqTHP6mymzuyubmW0dPjuBPyD&entinfo=32183489472458_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1c3nWnYn1bdPHTkrjmdXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KnWDLrjTzP1mOn1EvPWcKTHc3nWnYn1bdPHTkrjmdTHmknEDvnjDKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTHNKugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKujNkn1-6rHTVrH0YriYYP1KBsycznHnVPWNkmvndnhNYuhEdTHnkPHn1rjmYTHDOP1mknjmOnjcYnH9zPTDKrHDzPEDQTHT8PHEYnWTQrHDdnjDOnWD3rTDKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTEDQnjnKXLYKuhmvn1PhrHcvry7-nWcYm9&entinfo=28234395500865_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1nzP1czPHmzP19YrHNLXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KPHcLPWN1PWn1Pj0OrHnKTHnzP1czPHmzP19YrHNLTHmQnTDvnHTKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTHmKugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKnH7WnymkmHnVm1mvPiYYnWcQsyDdnhcVrAckPjTzmHwbmHTOTHndPWmYnHbkTHc1n1D3n1E1rjNQPjEQP9DKrHDzPEDQTHT8PHnYPHbkrHTzPWTQPHbYP9DKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTEDQnjnKXLYKPjFhmvmvPyPWmWFhmHc3PE&entinfo=32722562784957_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1nkrjmLrj9LnHT3PjDQXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KPjbkPHmvrHb3rjEkPjNKTHnkrjmLrj9LnHT3PjDQTHndnTD1PHTKnWnkrHEKnEDQPHDvP1TkrHNQP1ndTH0KugPx0APxpZPx0h-MpZEKnEDQTyRz0v6fIgwMg1cknH0krHTYgvR30ZkQTiYQTEDkTHTKTHDKuhNLrA7WnjNVuA7bPzYYnhE1sycdnANVPWKBuyF-uH9km1NLTHn1rjnYn1bdTHcQrjn1PW0kP1D1Pj0dn9DKrHDzPEDQTHT8rHc1P19LPWTLPWDLPW0YPEDKsEDKTy6YIZTlszqBpB3draOWUvYf0AFbUBtQskDKnHDYsWcYPz3dPB3QP1EKnTDkTEDQnjnKXLYKnW9OnH6-uyNkrHbvujm1uT&entinfo=30867887108411_q&slot=1000106&psid=134070767198797027142256658', 'http://jump.zhineng.58.com/jump?target=pZwY0jCfsvFJsWN3shPfUiqCIy-1pAqds1nzP1mdrjckrj03PWEQXaO1pZwVUTDQn1EkP1TLPW0QrH9LrH0knW0QPjczPHmvPH9KnHm3PjTYPjE3njN1rjcKTHnzP1mdrjckrj03PWEQTHnknkDYP9Dzn1TOPTDQTHDdnHmLnjTOPHDLn1NKrT7-0dqkmdqC0dqzpyICITDQTHDKugF1pAqdIAIxnWTQP1TOnjwxug6kxjDKsHDKTHTKnTDKnE7WnjDdnym1PaYLPj6-sHw-nAcVrjDdraYvP1wWrjTQuH03uynKn1NvrHD3nWNKnWn1PHcknHn1rjDvPH0vTEDOnHcdTHDKni3kPH9knH9OPHcOPjEknWn3TEDVTEDKpZwY0jCfsvFJsWN3shPfUiqkmhw8s1DfTEDQnHE8nWELsWNvsWDLPTDkTHTKTHDknk7exE7WmvP-PWPBmvmYPvcLrHb3&entinfo=32765820878641_q&slot=1000106&psid=134070767198797027142256658', 'http://bj.58.com/pingbandiannao/32854825016647x.shtml?psid=134070767198797027142256658&entinfo=32854825016647_0', 'http://bj.58.com/pingbandiannao/32854691151550x.shtml?psid=134070767198797027142256658&entinfo=32854691151550_0', 'http://bj.58.com/pingbandiannao/32847861193537x.shtml?psid=134070767198797027142256658&entinfo=32847861193537_0', 'http://bj.58.com/pingbandiannao/32847636415952x.shtml?psid=134070767198797027142256658&entinfo=32847636415952_0', 'http://bj.58.com/pingbandiannao/32847633443254x.shtml?psid=134070767198797027142256658&entinfo=32847633443254_0', 'http://bj.58.com/pingbandiannao/32847629834956x.shtml?psid=134070767198797027142256658&entinfo=32847629834956_0', 'http://bj.58.com/pingbandiannao/32846699523002x.shtml?psid=134070767198797027142256658&entinfo=32846699523002_0', 'http://bj.58.com/pingbandiannao/32846354124216x.shtml?psid=134070767198797027142256658&entinfo=32846354124216_0', 'http://bj.58.com/pingbandiannao/32846171014218x.shtml?psid=134070767198797027142256658&entinfo=32846171014218_0', 'http://bj.58.com/pingbandiannao/32846107855301x.shtml?psid=134070767198797027142256658&entinfo=32846107855301_0', 'http://bj.58.com/pingbandiannao/32845516314159x.shtml?psid=134070767198797027142256658&entinfo=32845516314159_0', 'http://bj.58.com/pingbandiannao/27225868111811x.shtml?psid=134070767198797027142256658&entinfo=27225868111811_0', 'http://bj.58.com/pingbandiannao/32835148189260x.shtml?psid=134070767198797027142256658&entinfo=32835148189260_0', 'http://bj.58.com/pingbandiannao/32832457409103x.shtml?psid=134070767198797027142256658&entinfo=32832457409103_0', 'http://bj.58.com/pingbandiannao/32826641072186x.shtml?psid=134070767198797027142256658&entinfo=32826641072186_0', 'http://bj.58.com/pingbandiannao/32821588203819x.shtml?psid=134070767198797027142256658&entinfo=32821588203819_0', 'http://bj.58.com/pingbandiannao/32816074810938x.shtml?psid=134070767198797027142256658&entinfo=32816074810938_0', 'http://bj.58.com/pingbandiannao/32809855236169x.shtml?psid=134070767198797027142256658&entinfo=32809855236169_0', 'http://bj.58.com/pingbandiannao/32800203473341x.shtml?psid=134070767198797027142256658&entinfo=32800203473341_0', 'http://bj.58.com/pingbandiannao/32800193946041x.shtml?psid=134070767198797027142256658&entinfo=32800193946041_0', 'http://bj.58.com/pingbandiannao/32799079287365x.shtml?psid=134070767198797027142256658&entinfo=32799079287365_0', 'http://bj.58.com/pingbandiannao/32779904523190x.shtml?psid=134070767198797027142256658&entinfo=32779904523190_0', 'http://bj.58.com/pingbandiannao/32777317349190x.shtml?psid=134070767198797027142256658&entinfo=32777317349190_0', 'http://bj.58.com/pingbandiannao/32776471372615x.shtml?psid=134070767198797027142256658&entinfo=32776471372615_0', 'http://bj.58.com/pingbandiannao/32770324808776x.shtml?psid=134070767198797027142256658&entinfo=32770324808776_0', 'http://bj.58.com/pingbandiannao/32770095911618x.shtml?psid=134070767198797027142256658&entinfo=32770095911618_0', 'http://bj.58.com/pingbandiannao/30510679893826x.shtml?psid=134070767198797027142256658&entinfo=30510679893826_0', 'http://bj.58.com/pingbandiannao/32767265756728x.shtml?psid=134070767198797027142256658&entinfo=32767265756728_0', 'http://bj.58.com/pingbandiannao/32767258209217x.shtml?psid=134070767198797027142256658&entinfo=32767258209217_0', 'http://bj.58.com/pingbandiannao/32767250902329x.shtml?psid=134070767198797027142256658&entinfo=32767250902329_0']

完整代码

遍历前一步得到的URL,对每个URL解析其中的标题、价格和时间信息。其中要注意的一点是有的商品是没有价格的,所以要进行判断。

import requests
from bs4 import BeautifulSoup

def get_links_from():
    urls = []
    list_view = 'http://bj.58.com/pbdn/1/'
    wb_data = requests.get(list_view)
    soup = BeautifulSoup(wb_data.text,'lxml')
    for link in soup.select('a.title.t'):
        urls.append(link.get('href'))
    return urls
# print(get_links_from())

def get_item_info():
    urls = get_links_from()
    for url in urls:
        wb_data = requests.get(url)
        soup = BeautifulSoup(wb_data.text,'lxml')

        title = soup.title.text
        price = soup.select('span.price.c_f50')[0].get_text() if len(soup.select('span.price.c_f50'))>0 else None
        time = soup.select('li.time')[0].get_text()
        data = {
            'title':title,
            'price':price,
            'time':time
        }
        print(data)

get_item_info()
{
    'title': '【图】(上门回收)苹果平板-笔记本-微软平板全系列IPad-Pro-mini4-Air2 - 平板电脑 - 北京58同城', 'price': '5000', 'time': '2016-05-19'}
{
    'title': '【图】《不止分期秒过 还能以旧换新》坚持给您最好的机器和服务 - 海淀北京大学平板电脑 - 北京58同城', 'price': '799', 'time': '2017-12-15'}
{
    'title': '【图】年底促销ipad-Pro新款10.5-苹果8-Mini4-可面交-可分期-可发货 - 平板电脑 - 东莞58同城', 'price': '1200', 'time': '2018-01-18'}
...
{
    'title': '【图】北京高价上门回收苹果平板 收购苹果笔记本 收购苹果手机 回收苹果笔记本 苹果pro笔记本 - 海淀平板电脑 - 北京58同城', 'price': '6666', 'time': '2018-01-15'}
{
    'title': '【图】回收苹果笔记本 苹果pro笔记本 苹果air笔记本回收 苹果电脑回收、苹果一体机回收 :回收苹果IP - 海淀平板电脑 - 北京58同城', 'price': '1500', 'time': '2018-01-15'}
{
    'title': '【图】北京高价上门回收笔记本 收购苹果笔记本 收购苹果手机 回收笔记本、回收苹果笔记本 苹果pro笔记本 - 海淀平板电脑 - 北京58同城', 'price': '666', 'time': '2018-01-15'}

你可能感兴趣的:(Python爬虫)