python爬虫:爬取新浪新闻数据

1. 爬虫的浏览器伪装原理:

我们可以试试爬取新浪新闻首页,我们发现会返回403 ,因为对方服务器会对爬虫进行屏蔽。此时,我们需要伪装成浏览器才能爬取。

1.实战分析:

浏览器伪装一般通过报头进行:

打开某个网页,按F12—Network— 任意点一个网址可以看到:Headers—Request Headers中的关键词User-Agent用来识别是爬虫还是浏览器。

import urllib.request

url='http://weibo.com/tfwangyuan?is_hot=1'
headers=('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0')
opener=urllib.request.build_opener()
opener.addheaders=[headers]
data=opener.open(url).read()

fh=open('F:/jupyterpycodes/python_pachongfenxi/result/王源微博个人中心.html','wb')
fh.write(data)
fh.close()

import urllib.request

url1='http://www.icourse163.org/home.htm?userId=1396403034#/home/course'
headers1=('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0')
opener=urllib.request.build_opener()
opener.addheaders=[headers1]
data1=opener.open(url1).read()

fh=open('F:/jupyterpycodes/python_pachongfenxi/result/慕课个人中心.html','wb')
fh.write(data1)
fh.close()

3. 新闻爬虫

3.1 需求:

将新浪新闻首页(http://news.sina.com.cn/)所有新闻都爬到本地

3.2 思路:

先爬首页,通过正则获取所有新闻链接,然后依次爬各新闻,再保存到本地

import urllib.request
import re


data=urllib.request.urlopen('http://news.sina.com.cn/').read()
data2=data.decode('utf-8','ignore')
pat='href="(https://news.sina.com.cn/.*?)">'
allurl=re.compile(pat).findall(data2) #匹配页面中所有的新闻超链接
for i in range(0,len(allurl)):
    try:
        print('第'+str(i)+'次爬取')
        thisurl=allurl[i]
        file='F:/jupyterpycodes/python_pachongfenxi/result/sinanews/'+str(i)+'.html'
        urllib.request.urlretrieve(thisurl,file)
        print('——成功——')
    except urllib.error.URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)

第0次爬取
404
Not Found
第1次爬取
——成功——
第2次爬取
——成功——
第3次爬取
——成功——
第4次爬取
——成功——
第5次爬取
——成功——
第6次爬取
——成功——
第7次爬取
——成功——
第8次爬取
——成功——
第9次爬取
——成功——
第10次爬取
——成功——
第11次爬取
——成功——
第12次爬取
——成功——
第13次爬取
——成功——
第14次爬取
——成功——
第15次爬取
——成功——
第16次爬取
——成功——
第17次爬取
——成功——
第18次爬取
——成功——
第19次爬取
——成功——
第20次爬取
——成功——
第21次爬取
——成功——
第22次爬取
——成功——
第23次爬取
——成功——
第24次爬取
——成功——
第25次爬取
——成功——
第26次爬取
——成功——
第27次爬取
——成功——
第28次爬取
——成功——
第29次爬取
——成功——
第30次爬取
——成功——
第31次爬取
——成功——
第32次爬取
——成功——
第33次爬取
——成功——
第34次爬取
——成功——
第35次爬取
——成功——
第36次爬取
——成功——
第37次爬取
——成功——
第38次爬取
——成功——
第39次爬取
——成功——
第40次爬取
——成功——
第41次爬取
——成功——
第42次爬取
——成功——
第43次爬取
——成功——
第44次爬取
——成功——
第45次爬取
——成功——
第46次爬取
——成功——
第47次爬取
——成功——
第48次爬取
——成功——
第49次爬取
——成功——
第50次爬取
——成功——
第51次爬取
——成功——
第52次爬取
——成功——
第53次爬取
404
Not Found
第54次爬取
404
Not Found
第55次爬取
404
Not Found
第56次爬取
404
Not Found
第57次爬取
404
Not Found
第58次爬取
404
Not Found
第59次爬取
404
Not Found
第60次爬取
404
Not Found
第61次爬取
404
Not Found
第62次爬取
404
Not Found
第63次爬取
404
Not Found
第64次爬取
404
Not Found
第65次爬取
404
Not Found
第66次爬取
404
Not Found
第67次爬取
404
Not Found
第68次爬取
404
Not Found
第69次爬取
404
Not Found
第70次爬取
404
Not Found
第71次爬取
404
Not Found
第72次爬取
404
Not Found
第73次爬取
404
Not Found
第74次爬取
404
Not Found
第75次爬取
404
Not Found
第76次爬取
404
Not Found
第77次爬取
404
Not Found
第78次爬取
404
Not Found
第79次爬取
404
Not Found
第80次爬取
404
Not Found
第81次爬取
404
Not Found
第82次爬取
404
Not Found
第83次爬取
404
Not Found
第84次爬取
404
Not Found
第85次爬取
404
Not Found
第86次爬取
404
Not Found
第87次爬取
404
Not Found
第88次爬取
404
Not Found
第89次爬取
404
Not Found
第90次爬取
404
Not Found
第91次爬取
404
Not Found
第92次爬取
404
Not Found
第93次爬取
404
Not Found
第94次爬取
404
Not Found
第95次爬取
404
Not Found
第96次爬取
404
Not Found
第97次爬取
404
Not Found
第98次爬取
404
Not Found
第99次爬取

`

发布了47 篇原创文章 · 获赞 35 · 访问量 1818

猜你喜欢

转载自blog.csdn.net/weixin_43412569/article/details/104854869