您好,登錄后才能下訂單哦!
python模擬游覽器爬取相關頁面
import urllib.request
url="https://blog.51cto.com/itstyle/2146899"
#模擬瀏覽器
headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
data=opener.open(url).read()
fh=open("D:/5.html","wb")
fh.write(data)
fh.close()
python爬取新聞網站并將文章下載到本地
import urllib.request
import urllib.error
import re
data=urllib.request.urlopen("http://news.sina.com.cn/").read()
data2=data.decode("utf-8","ignore")
pat='href="(http://news.sina.com.cn/.*?)">'
allurl=re.compile(pat).findall(data2)
for i in range(0,len(allurl)):
try:
print("第"+str(i+1)+"次爬取")
thisurl=allurl[i]
file="D:/pac/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)
python爬取月光博客文章下載到本地
import re
import urllib.request
import urllib.error
data=urllib.request.urlopen("http://www.williamlong.info/").read()
pat='rel="bookmark">(.*?)</a>'
pat_url='class="post-title"><a href="(http://www.williamlong.info/archives/.*?)"'
data=data.decode("utf-8")
mydata=re.compile(pat).findall(data) #所有的博客文章名
allurl=re.compile(pat_url).findall(data) #所有的網址鏈接
for i in range(0,len(allurl)):
try:
print("正在生產第"+str(i+1)+"次文件")
thisurl=allurl[i]
file="E:/PAS/yueguang/"+mydata[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)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。