抓取存儲在數(shù)據(jù)庫的鏈接的內(nèi)容

  • 下面是抓取信息的函數(shù)
def get_item_info(url):    
      web_data = requests.get(url)    
      soup = BeautifulSoup(web_data.text, 'lxml')    
      title = soup.select('h1.title-name')[0].text if soup.find_all('h1',{'class':'title-name'}) else None    
      if title == None:        
         pass    
      else:        
           data = {'time':list(soup.select('i.pr-5')[0].stripped_strings)[0].split()[0] if soup.find('i',{'class':'pr-5'}) else None, 
                   'type':soup.select('#wrapper > div.content.clearfix > div.leftBox > div:nth-of-type(3) > div > ul > li:nth-of-type(1) > span > a')[0].text if soup.find_all('ul',{'class':'det-infor'}) else None, 
                   'price':soup.select('i.f22.fc-orange.f-type')[0].text if soup.find_all('i',{'class':'f22 fc-orange f-type'}) else None,  
                   'address':list(map(lambda x:x.text,soup.select('#wrapper > div.content.clearfix > div.leftBox > div > div > ul > li:nth-of-type(3) > a'))) if soup.find_all('li') else None, 
                   'old_new':list(soup.select('#wrapper > div.content.clearfix > div.leftBox > div:nth-of-type(4) > div.det-summary > div > div.second-dt-bewrite > ul > li:nth-of-type(1)')[0].stripped_strings) if soup.select('#wrapper > div.content.clearfix > div.leftBox > div:nth-of-type(4) > div.det-summary > div > div.second-dt-bewrite > ul > li:nth-of-type(1)') else None }
#ul.det-infor > li:nth-of-type(1) > span 這個selector好像不能取下新舊程度        
          item_info.insert_one(data)        
          print(data)
  • 下面是調(diào)用上面函數(shù)的代碼

from get_third_url import get_item_info
from get_third_url import whole_third_url#whole_third_url是存儲鏈接的
from multiprocessing import Pool
import requests
if __name__ == '__main__':   
     pool = Pool()    
     try:       
        pool.map(get_item_info,whole_third_url.find(['url']))    
    except requests.exceptions.InvalidSchema:        
        pass

然后跑出來這么一個錯誤

Traceback (most recent call last):
  File "/Users/wangpegnfei/Desktop/Plan-for-combating-master/week2/week2_homework/myself/action2.py", line 10, in <module>
    pool.map(get_item_info,whole_third_url.find(['url']))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pymongo/collection.py", line 1137, in find
    return Cursor(self, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pymongo/cursor.py", line 121, in __init__
    validate_is_mapping("filter", spec)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pymongo/common.py", line 375, in validate_is_mapping
    "collections.Mapping" % (option,))
TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping

不知道是哪里出錯了,錯誤類型:過濾器必須是字典的一個實例

2016.5.20,更新一下,上面代碼有點問題,代碼有所更改

  • 下面是抓取信息的函數(shù)
def get_item_info(url, data=None):    
      wb_data = requests.get(url, headers=headers)    
     # 檢查頁面是否不存在,或者被封ip    
     if wb_data.status_code != 200:        
        return    
     soup = BeautifulSoup(wb_data.text, 'lxml')   
     prices = soup.select('.f22.fc-orange.f-type')   
     pub_dates = soup.select('.pr-5')    
     areas = soup.select('ul.det-infor > li:nth-of-type(3) > a')    
     cates = soup.select('ul.det-infor > li:nth-of-type(1) > span') 
     print(areas)    
     data = {'title': soup.title.text.strip(), 'price': prices[0].text.strip() if len(prices) > 0 else 0,  'pub_date': pub_dates[0].text.strip().split(' ')[0] if len(pub_dates) > 0 else "",  'area': [area.text.strip() for area in areas if area.text.strip() != "-"], 'cates': [cate.text.strip() for cate in cates], 'state': soup.select('ul.second-det-infor.clearfix > li')[0].text.split(':')[-1].strip() 
     if soup.find('ul','second-det-infor') and soup.select('ul.second-det-infor.clearfix > li')[0].text.split(':')[0].strip() == '新舊程度' else None,  'url': url    }   
     print(data)    
     item_info.insert_one(data)
  • 下面是調(diào)用上面函數(shù)的代碼

from get_third_url import get_item_info
from get_third_url import whole_third_url
from multiprocessing import Pool

if __name__ == '__main__':    
    pool = Pool()    
    for i in whole_third_url.find():        
    #print(i['url'])        
    pool.map(get_item_info,i['url'].split())
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容