Python 爬虫知识汇总 以及40*问题解决策略
来源:互联网 发布:英文seo教程 编辑:程序博客网 时间:2024/06/06 03:25
一、比较基础的:
2.7 爬虫爬取图片
#coding=utf-8
#urllib模块提供了读取Web页面数据的接口
import urllib
#re模块主要包含了正则表达式
import re
#定义一个getHtml()函数
def getHtml(url):
page = urllib.urlopen(url) #urllib.urlopen()方法用于打开一个URL地址
html = page.read() #read()方法用于读取URL上的数据
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext' #正则表达式,得到图片地址
imgre = re.compile(reg) #re.compile() 可以把正则表达式编译成一个正则表达式对象.
imglist = re.findall(imgre,html) #re.findall()方法读取html 中包含 imgre(正则表达式)的 数据
#把筛选的图片地址通过for循环遍历并保存到本地
#核心是urllib.urlretrieve()方法,直接将远程数据下载到本地,图片通过x依次递增命名
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'D:\E\%s.jpg' % x)
x+=1
html = getHtml("http://tieba.baidu.com/p/xxxx")
print getImg(html)
注意 2.7和3.6语法上有不同比如 2.7 print ‘x’ 但是 3.6 print(‘x’)
2.7 urllib2 3.6 urllibrequest
版本: Python 3.6
1.实现伪装window以及 firefox游览器
解决问题: 403问题
import urllib.request (2,7 urllib2)
chaper_url = "http://blog.csdn.net/eastmount/article/details/39599061"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
data = urllib.request.Request(url=chaper_url, headers=headers)
content = urllib.request.urlopen(data)
con = content. read();
#下面是负责解码
content = content.decode ('UTF-8','ignore')
//print(content)
open('test.html','w+').write(content)
获取网页的html
2.ssl安全证书的“免签”黄色以及下划线
以及 bs4的使用
beautifulsoup4 生成解析树 bs4 是对string进行处理 前面获取html字符串跟之前一样
import urllib.request
from bs4 import BeautifulSoup
import ssl
import re
context = ssl._create_unverified_context()
url = 'https://www.sec.gov/Archives/edgar/data/789019/000119312516742796/d245252d10q.htm'
data = urllib.request.urlopen(url,context=context).read()
data = data.decode('UTF-8','ignore')
soup = BeautifulSoup(data, "html.parser") #最好加上‘html.parser’告诉python这是个html文档
soup.find(id='xxx') # 寻找id属性为xxx的
soup.find(id=re.compile('xxx'),algin='xxx')
# 寻找id属性符合正则且algin属性为xxx的注意 xhtml1.0中所有algin已经不支持已经换为css的style
print (soup.find_all("a",limit=2)) #两个a标签
print('----')
print(soup.find_all('head')) #所有hea
bs4+字典指定特殊标签 style
s= soup.findAll(attrs={'style':r'text-align:right;font-variant: normal;'})
——————————————————————————————————————————————————————————
import urllib.request
from bs4 import BeautifulSoup
import ssl
context = ssl._create_unverified_context()
url = 'https://www.sec.gov/Archives/edgar/data/789019/000156459017000654/msft-10q_20161231.htm'
data = urllib.request.urlopen(url,context=context).read()
data = data.decode('UTF-8','ignore')
soup = BeautifulSoup(data, "html.parser")
print('----')
s= soup.findAll(attrs={'style':r'text-align:right;font-variant: normal;'})
s = str(s)
openfile = open('/Users/frankdura/Desktop/data.txt','w')
openfile.write(s)
openfile.close()
3.python request HTTPS报错54, 'Connection reset by peer有博主说这是因为:国家防火墙的原因。
下面我们用自己设置的header来伪装这个需要通过设置head来解决
import urllib.request
import sys
print (sys.getdefaultencoding() ) #这里是打印本系统的默认的编码
#下面是设置cookie
Cookie = "PHPStat_First_Time_10000011=1480428327337; PHPStat_Cookie_Global_User_Id=_ck16112922052713449617789740328; PHPStat_Return_Time_10000011=1480428327337; PHPStat_Main_Website_10000011=_ck16112922052713449617789740328%7C10000011%7C%7C%7C; VISITED_COMPANY_CODE=%5B%22600064%22%5D; VISITED_STOCK_CODE=%5B%22600064%22%5D; seecookie=%5B600064%5D%3A%u5357%u4EAC%u9AD8%u79D1; _trs_uv=ke6m_532_iw3ksw7h; VISITED_MENU=%5B%228451%22%2C%229055%22%2C%229062%22%2C%229729%22%2C%228528%22%5D"
url = http://search.time.com/?q=microsoft&site=time
#目标网址
# url = "http://www.hao123.com"
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'Cookie': Cookie,
'Connection': 'keep-alive',
'Accept': '*/*',
# 'Accept-Encoding': 'gzip, deflate, sdch',
#注意这里:有时候会出现乱码,单纯的 decode(‘utf-8’,’ignore’)无法解决问题
#这需要把header中的 'Accept-Encoding': 'gzip, deflate, sdch'改掉
'Accept-Language': 'zh-CN,zh;q=0.8',
'Host': 'query.sse.com.cn',
'Referer': 'http://www.sse.com.cn/assortment/stock/list/share/'
}
req = urllib.request.Request(url,None,headers)
#通过设置好的header来访问
response = urllib.request.urlopen(req)
the_page = response.read()
the_page = the_page.decode('ascii','ignore')
print(the_page)
正则之前可以先用split或者其他切片技术进行分割,方便套用正则表达式~成功!
如果需要翻墙爬取信息的话,需要使用不同代理:
使用pip安装socks负责代理访问的包
pip install pysocks
import requests
proxies = {'http':"socks5://myproxy:proxynum"}
requests.get('http://example.com', proxies=proxies)
或者更新自己的openssl:
由于Mac系统系统保护原因在不关闭系统保护的情况下无法卸载系统自带openssl,但是openssl版本不是最新的。
1.重启电脑,command+R进入恢复界面,打开终端输入在左上角工具条
$ csrutil disable #关闭系统文件保护
$ reboot #重启
2.重新启动电脑后,进入终端
$ sudo rm -rf /usr/bin/openssl #你系统原来的openssl的路径
3.使用homebrew安装openssl
$ brew update //有时候比较慢 需要给brew换源
$ brew install openssl
$ which openssl (检查是否成功安装)如何没有显示 –>的请看下面
检查目录
$ ln -s /usr/bin/{openssl/bin/,}openssl
$ openssl #检查是否成功
sudo ln -s /usr/local/Cellar/openssl/1.0.2k/bin/openssl /usr/bin/openssl
软链接到 可以作为命令行的bin目录左边是你brew 安的openssl不同电脑安装路径将会不同!
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
爬虫解决ip被封问题
在网络爬虫抓取信息的过程中,如果抓取频率高过了网站的设置阀值,将会被禁止访问。通常,网站的反爬虫机制都是依据IP来标识爬虫的。
1. 普通的基于ADSL拨号的解决办法(基础)
通常,在抓取过程中遇到禁止访问,可以重新进行ADSL拨号,获取新的IP,从而可以继续抓取。但是这样在多网站多线程抓取的时候,如果某一个网站的抓取被禁止了,
第一:用分布式高匿名 ,最好全国各地的IP。海量的IP,每天给你不重复IP,今天IP与昨天IP又不相同就最好了。阿布云代理还可以,我都试过,就是费用贵。质量都可以。真心推荐,不是打广告。阿布云代理 - 最专业、最稳定、IP最丰富的高匿名HTTP代理IP提供商
第二:别一天干掉一个站,慢点爬,设置下载延迟
第三:修改请求头,浏览器
第四:禁止Cookie
第五:你采取分布式爬取
作者:专注爬虫二十年
链接:https://www.zhihu.com/question/26018679/answer/118795804
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- Python 爬虫知识汇总 以及40*问题解决策略
- Python 爬虫解码问题解决
- Python爬虫技术汇总
- 【Python爬虫】入门知识
- python爬虫知识小结
- Python爬虫知识梳理
- python爬虫知识储备
- python列表知识汇总
- Python初学小知识汇总
- Python常用常见知识汇总
- Python 爬虫尽量不被发现策略
- Python爬虫入门 | 3 爬虫必备Python知识
- python爬虫进阶(七):应对反爬虫的策略
- Python爬虫-基于深度优先策略的百度百科爬虫
- python第一个爬虫小程序以及遇到问题解决(中文乱码)+批量爬取网页并保存至本地
- 读书笔记汇总 --- 用Python写网络爬虫
- Python基础学习-爬虫入门知识
- [踏石留印之爬虫] Python爬虫知识导图
- Pixhawk原生固件PX4之MAVLink外部通讯
- 写给自己
- POJ 3617 Best Cow Line(贪心+最小字典序)
- for...in 语句
- Android——自定义LinearLayout自动换行,TextView垂直排列。
- Python 爬虫知识汇总 以及40*问题解决策略
- 【干货】机器学习常用35大算法盘点
- SQL基础
- 使用Eclipse搭建Android开发环境
- 2016年(第15届)中国软件业务收入前百家企业名单(zz)
- Xen dom0激活xen_netback dynamic debug (netdev_dbg)
- Android中View.setPressed是怎么出现按压效果的
- MyBatis懒加载问题
- 类型转换的时候,.valueOf()和.parseX()的区别