Python探索之爬取电商售卖信息代码示例

yipeiwu_com6年前Python爬虫

网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本。

下面有一个示例代码,分享给大家:

#! /usr/bin/env python

#
encoding = 'utf-8'#
Filename: spider_58center_sth.py
from bs4
import BeautifulSoup
import time
import requests
url_58 = 'http://nj.58.com/?PGTID=0d000000-0000-0c5c-ffba-71f8f3f7039e&ClickID=1'
''
'
用于爬取电商售卖信息: 例为58同城电脑售卖信息 ''
'
def get_url_list(url):
  web_data = requests.get(url)
soup = BeautifulSoup(web_data.text, 'lxml')
url = soup.select('td.t > a[class="t"]')
url_list = ''
for link in url:
  link_n = link.get('href')
if 'zhuanzhuan' in link_n:
  pass
else :
  if 'jump' in link_n:
  pass
else :
  url_list = url_list + '\n' + link_n
print('url_list: %s' % url_list)
return url_list# 分类获取目标信息
def get_url_info():
  url_list = get_url_list(url_58)
for url in url_list.split():
  time.sleep(1)
web_datas = requests.get(url)
soup = BeautifulSoup(web_datas.text, 'lxml')
type = soup.select('#head > div.breadCrumb.f12 > span:nth-of-type(3) > a')
title = soup.select(' div.col_sub.mainTitle > h1')
date = soup.select('li.time')
price = soup.select('div.person_add_top.no_ident_top > div.per_ad_left > div.col_sub.summary > ul > '
  'li:nth-of-type(1) > div.su_con > span.price.c_f50')
fineness = soup.select('div.col_sub.summary > u1 > li:nth-of-type(2) > div.su_con > span')
area = soup.select('div.col_sub.summary > u1 > li:nth-of-type(3) > div.su_con > span')
for typei, titlei, datei, pricei, finenessi, areai in zip(type, title, date, price, fineness, area): #做字典
data = {
  'type': typei.get_text(),
  'title': titlei.get_text(),
  'date': datei.get_text(),
  'price': pricei.get_text(),
  'fineness': (finenessi.get_text()).strip(),
  'area': list(areai.stripped_strings)
}
print(data)
get_url_info()

爬取商城商品售卖信息

总结

以上就是本文关于Python探索之爬取电商售卖信息代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Python探索之自定义实现线程池Python探索之ModelForm代码详解等,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python抓取网页图片并放到指定文件夹

python抓取网站图片并放到指定文件夹 复制代码 代码如下:# -*- coding=utf-8 -*-import urllib2import urllibimport socket...

Python使用Srapy框架爬虫模拟登陆并抓取知乎内容

Python使用Srapy框架爬虫模拟登陆并抓取知乎内容

一、Cookie原理 HTTP是无状态的面向连接的协议, 为了保持连接状态, 引入了Cookie机制 Cookie是http消息头中的一种属性,包括: Cookie名字(Name)...

Python爬虫设置代理IP(图文)

Python爬虫设置代理IP(图文)

在爬虫的过程中,我们经常会遇见很多网站采取了防爬取技术,或者说因为自己采集网站信息的强度和采集速度太大,给对方服务器带去了太多的压力。 如果你一直用同一个代理ip爬取这个网页,很有可能i...

python实现爬虫下载美女图片

本次爬取的贴吧是百度的美女吧,给广大男同胞们一些激励 在爬取之前需要在浏览器先登录百度贴吧的帐号,各位也可以在代码中使用post提交或者加入cookie 爬行地址:http://tieb...

Python爬虫实现获取动态gif格式搞笑图片的方法示例

本文实例讲述了Python爬虫实现获取动态gif格式搞笑图片的方法。分享给大家供大家参考,具体如下: 有时候看到一些喜欢的动图,如果一个个取保存挺麻烦,有的网站还不支持右键保存,因此使用...