python学习之旅-第一周 实战作业

2016-05-19  本文已影响0人  张旭东0514

第一周实战作业
学习python的第一周 5.19号完成练习实战作业 爬去58同城平板电脑列表里的卖家信息

Paste_Image.png

目标:爬去卖家以及商品的基本信息
代码如下:

import requests
from bs4 import BeautifulSoup
import time
def get_message(url):
    #print(url)
    wb_date = requests.get(url)
    id = (url.split('/')[-1])[:14]#截取商品ID得到对应的id
    views = get_totols(id)
    soup = BeautifulSoup(wb_date.text,'lxml')
    categories = soup.select('#header > div.breadCrumb.f12 > span:nth-of-type(3) > a')#类目
    titles = soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.mainTitle > h1')#标题
    times = soup.select('#index_show > ul.mtit_con_left.fl > li.time')#发布时间
    prices = soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-of-type(1) > div.su_con > span')#价格
    olds = soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-of-type(2) > div.su_con > span')#成色
    areas1 = soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-of-type(3) > div.su_con > span > a:nth-of-type(1)')#区域1
    areas2 = soup.select('#content > div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.sumary > ul > li:nth-of-type(3) > div.su_con > span > a:nth-of-type(2)')#区域2
    for category,title,time,price,old,area1,area2 in zip(categories,titles,times,prices,olds,areas1,areas2):
        data = {
            '类目':category.get_text(),
            '标题':title.get_text(),
            '发布时间':time.get_text(),
            '价格':price.get_text(),
            '成色':old.get_text().strip(),
            '区域':area1.get_text()+'-'+area2.get_text(),
            '浏览量':views
        }
        print(data)
        return None
#去除列表中重复连接
def delRepeat(list):
    for x in list:
        while list.count(x)>1:
            del list[list.index(x)]
    #print(list)
    return list

#获取有效连接列表
def get_links(url):
    links = []
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text,'lxml')
    href = soup.select(' tr > td.t > a')
    for link in href:
        if 'http://bj.58.com/pingbandiannao/' in link.get('href'):#判断路径里是否有关键字  去除推荐的和转转的连接
            links.append(link.get('href'))#将连接加入到列表中去
    links = delRepeat(links)#去重
    for wb_link in links:
        get_message(wb_link)
        time.sleep(2)  # 防止反爬

#获取浏览量的方法
def get_totols(id):
    headers ={
            'Referer':'http://bj.58.com/pingbandiannao/25390255065933x.shtml?adtype=1&PGTID=0d305a36-0000-1576-ef5f-2547f6476ccf&entinfo=25390255065933_0&psid=132996494191831950071360497&iuType=q_2&ClickID=2',#58浏览量针对头部referer有判断  不伪造信息拿到值为0
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
        'Cookie':'f=n; userid360_xml=BE5F745CB21889493F55E2AB183C6316; time_create=1466153186464; bj58_id58s="ZGFzSVk5dkFyUDZQODU4OA=="; id58=c5/njVc0K1eW7SWOA1IbAg==; sessionid=895a33d3-6b04-4820-b906-89323e0a3419; als=0; myfeet_tooltip=end; bdshare_firstime=1463552754347; ipcity=sh%7C%u4E0A%u6D77; 58home=sh; f=n; bj58_new_session=0; bj58_init_refer=""; bj58_new_uv=3; 58tj_uuid=0f55bf86-b266-4866-aea3-3fed2ffe6ccf; new_session=0; new_uv=2; utm_source=; spm=; init_refer=http%253A%252F%252Fbj.58.com%252Fpbdn%252F0%252F; final_history=26044978248654%2C26044268155977%2C26013739776688'
    }
    api = 'http://jst1.58.com/counter?infoid={}&userid=&uname=&sid=516903216&lid=1&px=&cfpath=5,38484'.format(id)
    js = requests.get(api,headers=headers)
    views = js.text.split('=')[-1]
    #print(views)
    return views
full_url = ['http://bj.58.com/pbdn/{}/'.format(str(i)) for
            i in range(0, 10, 1)]
for link in full_url:
    get_links(link)

爬取结果:

Paste_Image.png

难点:本次项目是第一次比较全面的爬取项目,难点由以下几点
-.商品信息浏览量这个字段 58是用js来控制
-.抓取到这个JS的URL后发现58对这个URL做过反爬取 必须伪造头部信息中的Referer才能得到返回totol这个浏览量

总结:通过一周学习收获:由于编写本次抓取前,不知晓后面有讲解视频,自己一个人摸索,之后完成后再看了老师视频后发现有许多地方都能优化处理,但是这次的爬取也给自己增加了不少的信心和乐趣!

上一篇 下一篇

猜你喜欢

热点阅读