目的
zolデスクトップの壁紙をクロールし、写真をバッチでダウンロードします。
プロジェクトの準備
ソフトウェア:Pycharm
サードパーティライブラリ:requests、fake_useragent、re、lxml
ウェブサイトアドレス:http://desk.zol.com.cn/1920x1080/
プロジェクト分析
ウェブサイトを開いて見てください。
それぞれがアトラスです。
クリックし
てソースコードを表示すると
、それぞれがソースコードに含まれていることがわかります。静的なWebページとして決定されます。
ページ番号分析
最初のページのURLリンク:http://desk.zol.com.cn/1920x1080/1.html
2番目のページのURLリンク:http://desk.zol.com.cn/1920x1080/2.html
3番目のページのURLリンク:http ://desk.zol.com.cn/1920x1080/3.html
各ページが以下の番号で変化していることがわかります。
アンチクライミング分析
同じIPアドレスへの複数のアクセスは、ブロックされるリスクに直面します。ここでは、fake_useragentを使用して、アクセス用のランダムなUser-Agent要求ヘッダーを生成します。
コード
1.対応するサードパーティライブラリをインポートし、オブジェクトを継承するクラスを定義し、selfを継承するinitメソッドを定義し、selfを継承するmain関数mainを定義します。
import requests
from fake_useragent import UserAgent
import re
from lxml import etree
class bizhi(object):
def __init__(self):
self.url = 'http://desk.zol.com.cn/1920x1080/hot_{}.html'
ua = UserAgent(verify_ssl=False)
for i in range(1, 100):
self.headers = {
'User-Agent': ua.random
}
def main(self):
pass
if __name__ == '__main__':
spider = bizhi()
spider.main()
2.Webページを取得するためのリクエストを送信します。
def get_html(self,url):
response = requests.get(url, headers=self.headers)
html = response.content.decode('gb2312')
return html
注:html = response.content.decode( 'gb2312')
3。各アトラスのURLを取得します
def get_link(self,html):
target=etree.HTML(html)
links=target.xpath('//li[@class="photo-list-padding"]/a/@href')
for link in links:
print('http://desk.zol.com.cn'+link)
4.各ギャラリーの各写真のリンクを取得します
host='http://desk.zol.com.cn'+link
res = requests.get(host, headers=self.headers)
htm=res.text
images=re.compile('<img src="(.*?)" width="144" height="90" >').findall(htm)
for image in images:
print(image)
しかし、この方法で得られた画像は小さすぎます。
https://desk-fd.zol-img.com.cn/t_s144x90c5/g6/M00/0C/08/ChMkKV9PGV6IdfukAClWngY1Z3QAABx1wO9BUkAKVa2874.jpgの
サイズはわずか144x90です。
変更してみてください:https://desk-fd.zol-img.com.cn/t_s1920x1080c5/g6/M00/0C/08/ChMkKV9PGV6IdfukAClWngY1Z3QAABx1wO9BUkAKVa2874.jpg
それを開いて、[
OK]を確認します。
5.ローカルへのバッチダウンロード。
def get_link(self,html):
global filename
target=etree.HTML(html)
links=target.xpath('//li[@class="photo-list-padding"]/a/@href')
for link in links:
#print('http://desk.zol.com.cn'+link)
host='http://desk.zol.com.cn'+link
res = requests.get(host, headers=self.headers)
htm=res.text
#t=etree.HTML(htm)
#images=t.xpath('//div[@class="photo-list-box"]/ul/li/a/img/@src')
images=re.compile('<img src="(.*?)144x90(.*?)" width="144" height="90" >').findall(htm)
for image in images:
print(image[0]+'1920x1080'+image[1])
result_url=image[0]+'1920x1080'+image[1]
r=requests.get(result_url,headers=self.headers)
with open('F:/pycharm文件/photo/' + str(filename) + '.jpg', 'wb') as f:
f.write(r.content)
filename+=1
6.メイン関数と関数呼び出し。
def main(self):
end_page = int(input("要爬多少页:"))
for page in range(1, end_page + 1):
url = self.url.format(page)
print("第%s页。。。。" % page)
html=self.get_html(url)
self.get_link(html)
エフェクト表示
ローカルディレクトリを見てください。
完全なコードは次のとおりです。
import requests
from fake_useragent import UserAgent
import re
from lxml import etree
filename=0
class bizhi(object):
def __init__(self):
self.url = 'http://desk.zol.com.cn/1920x1080/hot_{}.html'
ua = UserAgent(verify_ssl=False)
for i in range(1, 100):
self.headers = {
'User-Agent': ua.random
}
def get_html(self,url):
response = requests.get(url, headers=self.headers)
html = response.content.decode('gb2312')
return html
def get_link(self,html):
global filename
target=etree.HTML(html)
links=target.xpath('//li[@class="photo-list-padding"]/a/@href')
for link in links:
#print('http://desk.zol.com.cn'+link)
host='http://desk.zol.com.cn'+link
res = requests.get(host, headers=self.headers)
htm=res.text
#t=etree.HTML(htm)
#images=t.xpath('//div[@class="photo-list-box"]/ul/li/a/img/@src')
images=re.compile('<img src="(.*?)144x90(.*?)" width="144" height="90" >').findall(htm)
for image in images:
print(image[0]+'1920x1080'+image[1])
result_url=image[0]+'1920x1080'+image[1]
r=requests.get(result_url,headers=self.headers)
with open('F:/pycharm文件/photo/' + str(filename) + '.jpg', 'wb') as f:
f.write(r.content)
filename+=1
def main(self):
end_page = int(input("要爬多少页:"))
for page in range(1, end_page + 1):
url = self.url.format(page)
print("第%s页。。。。" % page)
html=self.get_html(url)
self.get_link(html)
if __name__ == '__main__':
spider = bizhi()
spider.main()
免責事項:それは自習のための参照としてのみ使用されます。