≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python爬虫最简单代码

人气:831 时间:2019-01-20

这篇文章主要为大家详细介绍了python爬虫最简单代码,具有一定的参考价值,可以用来参考一下。

感兴趣python爬虫最简单代码的小伙伴,下面一起跟随四海网的小编罗X来看看吧。<br>
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import cookielib
import urllib2
from urllib import urlencode
import MySQLdb
from bs4 import BeautifulSoup

# 数据库
mydb = MySQLdb.connect(host="10.0.66.248", user="root", passwd="123456", db="invCloudOA", charset="utf8")
cursor = mydb.cursor()
mydb.autocommit(on="on")

# 建一个cookie处理器的opener
cookie = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
conn = urllib2.build_opener(handler)

# 请求参数
param = {
    'email': '88888888@qq.com',
    'password': '88888888',
    'target': 'http://aaaaaa.com/member'
}

# 发送请求
resp = conn.open("http://aaaaaa.com/user/login", data=urlencode(param))

# 登录成功后,开始采集数据
for i in xrange(1, 200, 1):
    print("当前页码 %d" % i)
    try:
        resp_con = conn.open("http://aaaaaa.com/member/index/page/%d" % i, timeout=20)
        resp_string = resp_con.read()
        bs = BeautifulSoup(resp_string)

        a_list = bs.select('li[class="col-sm-6 col-md-4 col-lg-6"] a')
        try:
            for a in a_list:
                person_html = conn.open("http://aaaaaa.com%s" % a.get("href"), timeout=20)
                person_soup = BeautifulSoup(person_html)

                person_detail_soup = person_soup.find('div', class_='col-sm-12 col-md-8 detail-left min-padding')

                try:
                    username = person_detail_soup.find('div', class_='detail').find('div', class_='introduce').find('h4').find('span').get_text()
                except:
                    username = ""

                try:
                    gongsi = person_detail_soup.find('div', class_='detail').find('div', class_='introduce').find('div', class_="local").find('a').get_text()
                except:
                    gongsi = ""

                try:
                    zhiwu = person_detail_soup.find('div', class_='detail').find('div', class_='introduce').find_all('div', class_="local")[1].get_text()
                except:
                    pass
                try:
                    diqu = person_detail_soup.find('div', class_='detail').find('div', class_='introduce').find_all('div', class_="local")[2].find('a').get_text()
                except:
                    diqu = ""
                try:
                    shouji = person_detail_soup.find_all('div', class_='contacts')[0].get_text()
                except:
                    shouji = ""
                try:
                    qq = person_detail_soup.find_all('div', class_='contacts')[1].get_text()
                except:
                    qq = ""
                try:
                    weibo = person_detail_soup.find_all('div', class_='contacts')[2].get_text()
                except:
                    weibo = ""
                try:
                    email = person_detail_soup.find_all('div', class_='contacts')[3].get_text()
                except:
                    email = ""
                try:
                    weixin = person_detail_soup.find_all('div', class_='contacts')[4].get_text()
                except:
                    weixin = ""
                sql = "INSERT INTO`ft_person`(`mingzi`,`shouji`, `qq`, `weibo`, `emai`, `wexin`, `chanpinjingli`, `zhiyejingli`, `gerenjieshao`, `yuanid`,gongsi,zhiwu,diqu) VALUES('%s', '%s', '%s','%s' ,'%s', '%s','%s', '%s', '%s', '%s','%s','%s','%s');" % (
                    username, shouji, qq, weibo, email, weixin, "", "", "", a.get("href"), gongsi, zhiwu, diqu)
                cursor.execute(sql)
                print("采集成功 %s" % username.encode('utf-8'))
        except StandardError as ex:
            print(ex)
            print("采集该人物失败 %s" % a.get("href"))
    except StandardError as ex:
        print(ex)
        print("采集第 %d 页失败!" % i)
# 关闭数据库
cursor.close()
 

本文来自:http://www.q1010.com/181/2938-0.html

注:关于python爬虫最简单代码的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:爬虫

您可能感兴趣的文章

  • python简单爬虫代码
  • 有趣的python爬虫项目
  • python微博爬虫实例
  • python小实例一:简单爬虫
  • python爬虫简单实例
  • python爬虫获取百度搜索结果的简单示例
  • python解决网站爬虫限制的简单示例
  • python爬虫代码示例
  • Python 天气爬虫采集器的简单示例
  • python 简单爬虫实现方法
上一篇:python简单爬虫代码
下一篇:python爬取网页数据
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。