美文网首页
抓取存储在数据库的链接的内容

抓取存储在数据库的链接的内容

作者: 鸣人吃土豆 | 来源:发表于2016-05-19 11:13 被阅读634次
  • 下面是抓取信息的函数
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)
  • 下面是调用上面函数的代码

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,更新一下,上面代码有点问题,代码有所更改

  • 下面是抓取信息的函数
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)
  • 下面是调用上面函数的代码

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())

相关文章

  • 抓取存储在数据库的链接的内容

    下面是抓取信息的函数 下面是调用上面函数的代码 然后跑出来这么一个错误 不知道是哪里出错了,错误类型:过滤器必须是...

  • 爬虫实战

    爬虫介绍 调度器 URL管理器(用来存储待抓取的链接,已经抓取过的链接) 网页下载器(消费URL管理器中待抓取的链...

  • 关键词——用到极致就是绝招

    引读:什么是Spider? Spider抓取系统包括链接存储系统、链接选取系统、dns解析服务系统、抓取调度系统、...

  • 使用ReactiveCocoa开发RSS阅读器

    目前已经完成的功能有对RSS的解析和Atom解析,RSS内容本地数据库存储和读取,抓取中状态进度展示,标记阅读状态...

  • 第二周/第二节练习项目: 爬取手机号

    1. 引言 抓取58同城中的手机号类目下所有帖子的标题和链接, 并将结果存储在mongodb中 取出存储在mong...

  • 你所不了解的搜索引擎

    搜索引擎指的是蜘蛛程序沿着链接爬行和抓取网上的大量网页内容,存入数据库,经过复杂的算法进行预处理,建立网站索引目录...

  • 使用jsoup抓取简书的内容

    抓取的是我博客的文章标题和链接,使用map存储,直接附上代码: 输出如下:

  • webscraper 4个Sitemap

    一、抓取公众号标题、时间、内容链接 {"_id":"gongzhonghao","startUrl":["http...

  • 第一步: 1、爬行抓取 蜘蛛根据网站的网址链接抓取链接所对应的页面 重点收集的是页面的文本内容。(站长工具里面机器...

  • Nebula 架构剖析系列(二)图数据库的查询引擎设计

    摘要 上文(存储篇)说到数据库重要的两部分为存储和计算,本篇内容为你解读图数据库 Nebula 在查询引擎 Que...

网友评论

      本文标题:抓取存储在数据库的链接的内容

      本文链接:https://www.haomeiwen.com/subject/clqgrttx.html