week2-爬取赶集网10万商品数据

2016-05-06  本文已影响0人  littleY

学习Python爬虫的第二周,完成了爬取赶集网二手市场的10万商品数据。

成果:
url_list.png
item_info.png
代码:

*channel_extract.py *

from bs4 import BeautifulSoup
import requests

# spider1
start_url = 'http://bj.ganji.com/wu/'
url_host= 'http://bj.ganji.com'
def get_channel_urls(url):
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('dl.fenlei dt > a ')
    for link in links:
        page_url = url_host + link.get('href')
        print(page_url)
get_channel_urls(start_url)

channel_list = '''
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/
'''

page_parsing.py

from bs4 import BeautifulSoup
import requests
import time
import pymongo

client = pymongo.MongoClient('localhost', 27017)
ceshi = client['ceshi']
url_list = ceshi['url_list1']
item_info = ceshi['item_info1']

#随机UA
userAgent = random.choice(['Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36', 
                           'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36', 
                           'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36', 
                           'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36',  
                           'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',   
                           'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
                            ])
# spider2
def get_links_from(channel, pages, who_sells='o'):
    headers = {
        'User-Agent': userAgent,
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept - Encoding': 'gzip, deflate, sdch',
        'Accept - Language': 'zh - CN, zh;q = 0.8, en;q = 0.6',
        'Cache - Control': 'max - age = 0','Connection': 'keep - alive'
    }
    client = pymongo.MongoClient('localhost', 27017)
    ceshi = client['ceshi']
    url_list = ceshi['url_list1']
    list_view = '{}{}{}/'.format(channel, str(who_sells), str(pages))
    wb_data = requests.get(list_view, headers=headers)

    #随机访问延时
    i = random.randrange(0, 3)
    time.sleep(i)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('li.js-item > a')
    if soup.find('ul', 'pageLink'):
        for link in links:
            item_link = link.get('href')
            headers = requests.head(item_link, allow_redirects=False).headers
            if headers['Server'] == 'nginx':
                item_link = headers['Location']
                url_list.insert_one({'url': item_link})
            else:
                url_list.insert_one({'url': item_link})
                pass
            get_item_info(item_link)
    else:
        pass
        
def get_item_info(url):
    headers = {
        'User-Agent': userAgent,
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept - Encoding': 'gzip, deflate, sdch',
        'Accept - Language': 'zh - CN, zh;q = 0.8, en;q = 0.6',
        'Cache - Control': 'max - age = 0','Connection': 'keep - alive'
    }
    client = pymongo.MongoClient('localhost', 27017)
    ceshi = client['ceshi']
    item_info = ceshi['item_info1']
    wb_data = requests.get(url, headers=headers)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    no_longer_exist = soup.find('div', 'error')
    if no_longer_exist:
        pass
    else:
        title = soup.select('.title-name')[0].text
        date = soup.select('.pr-5')[0].text.split('发布')[0].strip() if soup.find('i', 'pr-5') else None
        cate = soup.select('.det-infor > li > span > a')[0].text
        price = soup.select('.f22.fc-orange.f-type')[0].text
        loco_list = list(soup.select('div.leftBox > div:nth-of-type(3) > div > ul > li:nth-of-type(3) > a'))
        area = []
        for loco in loco_list:
            area.append(loco.text)
        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
        item_info.insert_one({'title': title, 'date': date, 'cate': cate, 'price': price, 'area': area, 'state': state, 'url': url})

main.py

from multiprocessing import Pool
from channel_extract import channel_list
from page_parsing import get_links_from, url_list, item_info, get_item_info

def get_all_links_from(channel):
    for num in range(1, 100):
        get_links_from(channel, num)
if __name__ == '__main__':
    pool = Pool()
    pool.map(get_all_links_from, channel_list.split())
        # 断点续传
    db_urls = [item['url'] for item in url_list.find()]
    index_urls = [item['url'] for item in item_info.find()]
    x = set(db_urls)
    y = set(index_urls)
    rest_of_urls = x - y
    pool.map(get_item_info, rest_of_urls)

counts.py

import time
from page_parsing import url_list, item_info

while True:
    print(url_list.find().count())
    print(item_info.find().count())
    print('\n')
    time.sleep(10)
总结:
上一篇下一篇

猜你喜欢

热点阅读