进行大数据爬取数据,存入Mongodb

爬虫刚入门,对赶集网进行一次所有商品大数据的爬取

使用了多线程,存到数据库里,目前不知道要爬多久

有一个要注意的地方,比如我们要进行两次爬虫,一次是把每个项目的链接爬下来,一次是把每个项目里的详情信息爬下来,最好是先把每个项目的链接存下来,并用数据库保存,然后再从数据库取出每个链接,进行爬取详情信息。不要每爬到一个链接,就进去把这个链接的信息爬出来,这样不仅速度变慢,往往在爬取详情信息过程中会发生网络超时等断开爬虫的情况,如果是每次爬取链接都去取信息,那么一旦爬虫断了,每次都要重新开始





from multiprocessing import Pool
from channel_extract import channel
from page_parsing import get_item_info, get_links

def get_all_link_from(channel):
    for i in range(1, 100):
        get_links(channel, i)

if __name__ == '__main__':
    pool = Pool()
    pool.map(get_all_link_from, channel.split())
    pool.close()
    pool.join()



import time
from page_parsing import url_list

while True:
    print(url_list.find().count())
    time.sleep(5)

from bs4 import BeautifulSoup
import requests
import time

url = 'http://bj.ganji.com/wu/'
host_url = 'http://bj.ganji.com'
def get_channel(url):
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('div > div > dl > dt > a')
    for link in links:
        new_url = host_url + link.get('href')
        print(new_url)

get_channel(url)


channel = '''
http://bj.ganji.com/jiaju/
http://bj.ganji.com/rirongbaihuo/
http://bj.ganji.com/shouji/
http://bj.ganji.com/shoujihaoma/
http://bj.ganji.com/bangong/
http://bj.ganji.com/nongyongpin/
http://bj.ganji.com/jiadian/
http://bj.ganji.com/ershoubijibendiannao/
http://bj.ganji.com/ruanjiantushu/
http://bj.ganji.com/yingyouyunfu/
http://bj.ganji.com/diannao/
http://bj.ganji.com/xianzhilipin/
http://bj.ganji.com/fushixiaobaxuemao/
http://bj.ganji.com/meironghuazhuang/
http://bj.ganji.com/shuma/
http://bj.ganji.com/laonianyongpin/
http://bj.ganji.com/xuniwupin/
http://bj.ganji.com/qitawupin/
http://bj.ganji.com/ershoufree/
http://bj.ganji.com/wupinjiaohuan/
'''


from bs4 import BeautifulSoup
import requests
import time
import pymongo
import random

client = pymongo.MongoClient('localhost', 27017)
ganji = client['gangji']
url_list = ganji['url_list']
item_info = ganji['item_info']

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36',
    'Connection':'keep-alive'
}

proxy_list = [
    'http://101.4.136.34:81',
    'http://182.96.194.184:8118'
]
proxy_ip = random.choice(proxy_list)
proxies = {'http':proxy_ip}


def get_links(channel,  page):
    list_view = '{}o{}'.format(channel, page)
    wb_data = requests.get(list_view)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('td.t > a.t')
    if(soup.find('td', 't')):
        for link in links:
            new_url = str(link.get('href').split('?')[0])
            url_list.insert_one({'url':new_url})
    else:
        pass


def get_item_info(url):
    wb_data = requests.get(url, headers = headers, proxies = proxies)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    no_longer_exist = (wb_data.status_code == 404)
    if no_longer_exist:
        pass
    else:
        try:
            title = soup.title.text
            price = soup.select('span.price_now > i')[0].get_text()
            area = soup.select('div.palce_li > span > i')[0].get_text()
            data = {
                'title':title,
                'price':price,
                'area' :area
            }
            print(data)
        except (AttributeError, IndexError):
            pass








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值