Pythonの爬虫類:シーナニュースクロールデータ

1.爬虫類ブラウザ迷彩原則:

私たちは、他のサーバーは、シールドを爬虫類ますので、我々は、403が返されますことを発見し、シーナニュースをクロールしようとすることができます。この時点で、私たちは、クロールにブラウザにふりをする必要があります。

1.実際の分析:

ブラウザの迷彩は、一般的に、ヘッダーで行われます。

Webページを開き、プレスF12-ネットワーク - は、あなたが見ることができるURLの任意のポイント:ヘッダ・リクエストヘッダのキーワードの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