(一)爬虫综览

一、网络数据采集

涉及内容:
数据库、网络服务器、HTTP协议、HTML语言、网络安全、图像处理、数据科学等内容。

二、开发环境

  • Python版本:Python3.5
  • 操作系统 : Win10
  • IDE : 系统IDLE 、Pycharm

三、相关库

  • urllib
  • requests
  • selenium
  • re
  • BeautifulSoup
  • selenium
  • threading 、 muitiprocess
  • mysql.connector
  • xlsxwriter
  • scrapy

四、步骤工具

(一) 获取网页,请求数据

  • urllib
  • requests
  • selenium
1、
from urllib.request import urlopen
html = urlopen(url)
# html.read()    为未处理的二进制网页源代码
soup = BeautifulSoup(html.read(), 'lxml') #网页源代码
#加不加 .read 好像没差
2、
import requests
html = requests.get(url, headers=headers)
# html.text  为未处理的网页源代码
soup = BeautifulSoup(html.text, 'lxml')       
# 得加 .text
3、
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get(url)
driver.page_source #为未处理的网页源代码

(二) 提取内容

  • re
  • BeautifulSoup
  • selenium
1、

re 匹配

2、

BeautifulSoup
find、 find_all、 select
网页标签,selector

3、

find_elements...
网页标签,selector,xpath

(三) 下载内容

  • open...write
  • urllretrieve
  • requests
下载图片等文件
1、
from urllib.request import urlretrieve
urlretrieve(url, filename=None, reporthook=None, data=None)
2、
html = urlopen(imageUrl)
data = html.read()
f= open(fileName,'wb')
f.write(data)
f.close()
3、
import requests
picture = requests.get(url, headers=headers)
        if picture.status_code == 200: 
        open(path, 'wb').write(picture.content)

(四) 储存

  • csv
  • xlsxwriter
  • mysql.connector
  • pymongdb

(五) 模拟浏览器

selenium
splinter

(六) 中级爬虫框架

  • scrapy
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容