Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1825|回复: 6

Python3网络爬虫实战

[复制链接]

30

主题

102

帖子

372

积分

认证用户组

Rank: 5Rank: 5

积分
372
发表于 2019-4-24 10:02:47 | 显示全部楼层 |阅读模式
本帖最后由 hshy 于 2019-4-25 08:45 编辑

爬虫常用库
urllib
rullib.request

requests

selenium


下载
chromedriver.exe

from selenium import webdriver
driver = ebdriver.chrome()
driver.get('http://www.baidu.com')
driver.page_soure

phantomjs
回复

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-3 22:34:21 | 显示全部楼层
本帖最后由 java 于 2019-5-3 22:55 编辑

with open('/var/tem/1.gif','wb') as f;      f.write(response.content)
       f.close()


解析方式:
直接处理
Json解析
正则表达式
BeautifulSoup
PyQuery
XPath


python3
import requests
request.get('http://m.weibo.com').text

如何解决javascript渲染的问题?
1.分析Ajax请求
2.Selenium/WebDriver ( 模拟浏览器加载)
from selenium import webdriver
driver = webdriver.chrome()
driver.get('http://www.baidu.com')
print(driver.page_soure)  和web浏览器中的源码数据一致


3.Splash   模拟js渲染

4.PyV8 、 Ghost.py  等其它库模拟加载js

怎样保存数据?
1.文本
2.关系型数据库      工具  DataGrip
3.非关系型数据库   工具  Robomongo
MongoDB  Redis等Key-Value4.二进制文件


回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-4 09:46:05 | 显示全部楼层
本帖最后由 java 于 2019-5-4 11:15 编辑

Urllib Python内置的Http请求库urllib.request  请求模块
urllib.error  异常处理模块
urllib.parse  url解析模块
urllib.robotparser  robots.txt解析模块

与Python2的变化
Python2
import urllib2
response = urllib2.urlopen('http://www.baidu.com')

Python3
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')


什么是Requests
Request是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。
它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。
一句话---Python实现的简单易用的HTTP库
安装 pip install requests

回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-4 12:15:19 | 显示全部楼层
正则表达式
非Python独有,re模块实现

http://tool.oschina.net/regex  在线正则工具

回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-4 12:19:56 | 显示全部楼层
BeautifulSoap
灵活又方便的网页解析库,处理高效,支持多种解析器。
利用它不用编写正则表达式即可方便地实现网页信息的提取。

安装
pip install beautifulsoup4


回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-4 12:59:47 | 显示全部楼层
本帖最后由 java 于 2019-5-4 13:07 编辑

PyQuery
强大又灵活的网页解析库。如果你觉得正则写起来太麻烦,
如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,
那么PyQuery就是你的绝佳选择。

安装  pip install pyquery

from pyquery import PyQuery as pq
doc = pq(html)
print(doc('li'))

https://pyquery.readthedocs.io/en/latest/api.html

更多CSS内容:http://www.w3school.com.cn/css/index.asp


回复 支持 反对

使用道具 举报

697

主题

1142

帖子

4086

积分

认证用户组

Rank: 5Rank: 5

积分
4086
发表于 2019-5-4 13:09:39 | 显示全部楼层
本帖最后由 java 于 2019-5-4 16:06 编辑

Selenium
自动化测试工具,支持多种浏览器。
爬虫中主要用来解决JavaScript渲染的问题。

安装pip install selenium

更多selenium使用方法  https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

下载 chromedriver.exe
http://chromedriver.chromium.org/
https://chromedriver.storage.googleapis.com/index.html

放到环境变量所包含的路径下如:
D:\ProgramFiles\Anaconda3\Scripts 目录下


selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 74
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)

查看自己的chrome版本,下载对应版本的chromedriver.exe
链接:https://pan.baidu.com/s/1hCNGhke3MNZ8KA9OCaJlbA
提取码:bguh



这时升级一下chrome到74版本
链接:https://pan.baidu.com/s/1uyyBL2XUW7zXUqusU81vyg
提取码:5vdp
安装无界面浏览器 Phantomjs

http://phantomjs.org/
链接:https://pan.baidu.com/s/1-ncbuB9rGBGLdcCu8fFS1Q
提取码:4vkn



回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-4-19 09:09 , Processed in 0.070123 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表