python大作业思路_Python实战计划——第一周大作业

抓取58商品信息

未解决:无法有效分辨推广信息和正常信息(肉眼可以分辨,但是无法通过链接或标签中的属性特征来分辨)。

代码如下:

#-*- coding: utf-8 -*-

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,一共15位

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(1) # 防止反爬技巧之一

#获取浏览量的方法

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有判断的应对,人工访问信息,防止反爬技巧之二

'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36',

'Cookie':'bj58_id58s="c19BMUtfcUdITnA1ODU4Mw=="; id58=c5/njVc56DRPBRJ9BO59Ag==; als=0; __utma='

'253535702.423222974.1463666994.1463666994.1463666994.1; __utmz=253535702.1463666994.1.1.utmcsr='

'(direct)|utmccn=(direct)|utmcmd=(none); 58home=wuhai; myfeet_tooltip=end; bangbigtip2=1; ipcity='

'wuhai%7C%u4E4C%u6D77; 58tj_uuid=0c52f1f5-7b9b-4dd7-81af-d2bb5426d2a9; new_session=0; new_uv=3; '

'utm_source=; spm=; init_refer=http%253A%252F%252Fbj.58.com%252Fpbdn%252F0%252F; final_history='

'26070179017526%2C25390255065933%2C24063857671738%2C26057897956784%2C25896821493035; sessionid='

'7d224bdd-5534-4102-acbc-9a43514cf79e; bj58_new_session=0; bj58_init_refer="http://bj.58.com/pbdn/0/"; '

'bj58_new_uv=38'

}

api = 'http://jst1.58.com/counter?infoid={}&userid=&uname=&sid=516903216&lid=1&px=&cfpath=5,38484'.format(id)

#这里使用了api接口

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)]#抓取前10页

for link in full_url:

get_links(link)

python大作业 一、Turtle创意大PK 自拟题目,完成一个利用Python程序的创意绘图,采用turtle库绘图为主,不少于50行代码,可选采用其他库。 (滑稽绘制) 二、程序练习 2.1 问题描述(10分) 人们常常提到"一万小时定律",就是不管你做什么事情,只要坚持一万小时,应该都可以成为该领域的专家。那么,10000小时是多少年多少天呢? 2.2 问题描述(10分)0380031003800341590145037657 编写计算从n到m和的函数‬,函数名为sum(n,m)‬,函数返回值为n到m所有数据的和‬,使用该函数计算输入数据x,y之间所有数据的和。 2.3 问题描述(15分) 编写函数judgeTri(a,b,c),判断以参数a,b,c的值为边长能否构成三角形并判断三角形的形状;若是锐角三角形,返回R;若是直角三角形,返回Z;若是钝角三角形,返回D;若三边长不能构成三角形,返回ERROR。 2.4 问题描述(15分) 用户输入一个字符串,分别统计其中小写字母、大写字母、数字、空格和其他字符的个数,并在一行内输出小写字母、大写字母、数字、空格和其他字符的个数。 2.5 问题描述(20分) 程序的功能: (1) 使用随机库功能,生成一个包含10个不重复且小于200的正整数列表ls1,输出ls1。‬ (2) 使用列表排序方法,对ls1按奇数在前偶数在后,并且奇数之间的相对顺序不变,偶数之间的相对顺序也不变进行排序,再输出ls1。‬ (3) 使用列表排序方法,对ls1按元素字符长度降序进行排序,输出ls1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值