生活随笔
收集整理的這篇文章主要介紹了
Project 3 :Python爬虫源码实现抓取1000条西刺免费代理IP-HTTPS并保存读取
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
基于python3.6—pycharm編寫的,都已經(jīng)調(diào)試過了,歡迎大家提出交流意見。
關(guān)于一些基礎(chǔ)的數(shù)據(jù)網(wǎng)頁分析(比如為什么是這個(gè)url)沒有過多贅述,看不懂的自行百度。
源碼可以直接拷貝運(yùn)行。
轉(zhuǎn)載請(qǐng)注明來源。
import requests
import re
'''
Python爬蟲源碼實(shí)現(xiàn)抓取1000條西刺免費(fèi)代理IP-HTTPS并保存讀取
'''
#抓取代理IP函數(shù)
def get_https():
for i
in range(
0,
10):url =
"http://www.xicidaili.com/wn/%d"%(i+
1)
#構(gòu)建請(qǐng)求目標(biāo)URL
#構(gòu)建請(qǐng)求頭
headers = {
'Host':
'www.xicidaili.com',
'Referer':
'http://www.xicidaili.com/',
'Upgrade-Insecure-Requests':
'1',
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}resp = requests.get(url,
headers=headers)resp.encoding = resp.apparent_encoding
#設(shè)置編碼為網(wǎng)頁編碼
result = resp.texthttps = re.findall(
r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", result)
#正則表達(dá)式提取IP地址
ports = re.findall(
r"<td>(\d+)</td>", result)
#正則表達(dá)式提取端口
#創(chuàng)建文件保存數(shù)據(jù)
with open(
'https.txt',
'a',
encoding=
'utf-8')
as f:
for x
in range(
100):f.write(
"https://"+https[x]+
":"+ports[x]+
"\n")
#組合地址和端口,獲取完整的IP地址
print(
'正在寫入第',
100*i+x+
1,
'條')
#構(gòu)建讀取抓取到的代理IP函數(shù)
def read_https():
with open(
'https.txt',
'r',
encoding=
'utf-8')
as f:line = f.read().strip(
'\n')
print(line)
if __name__ ==
'__main__':get_https()read_https()
總結(jié)
以上是生活随笔為你收集整理的Project 3 :Python爬虫源码实现抓取1000条西刺免费代理IP-HTTPS并保存读取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。