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

yipeiwu_com7年前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通过链接抓取网站详解

在本篇文章里,你将会学习把这些基本方法融合到一个更灵活的网站 爬虫中,该爬虫可以跟踪任意遵循特定 URL 模式的链接。 这种爬虫非常适用于从一个网站抓取所有数据的项目,而不适用于从特...

python用BeautifulSoup库简单爬虫实例分析

会用到的功能的简单介绍 1、from bs4 import BeautifulSoup #导入库 2、请求头herders headers={'User-Agent': 'Mo...

python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析

字符串常用方法 # 去掉左右空格 'hello world'.strip() # 'hello world' # 按指定字符切割 'hello world'.split(' ')...

Python实现抓取城市的PM2.5浓度和排名

Python实现抓取城市的PM2.5浓度和排名

主机环境:(Python2.7.9 / Win8_64 / bs4) 利用BeautifulSoup4来抓取 www.pm25.com 上的PM2.5数据,之所以抓取这个网站,是因为上面...

python 爬取微信文章

本人想搞个采集微信文章的网站,无奈实在从微信本生无法找到入口链接,网上翻看了大量的资料,发现大家的做法总体来说大同小异,都是以搜狗为入口。下文是笔者整理的一份python爬取微信文章的代...