python简单爬虫笔记

python模拟游览器爬取相关页面

创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目网站建设、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元西宁做网站,已为上家服务,为西宁各地企业和个人服务,联系电话:18982081108

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)

文章题目:python简单爬虫笔记
分享链接:https://www.cdcxhl.com/article46/jodseg.html

成都网站建设公司_创新互联,为您提供微信公众号网站设计公司外贸网站建设企业网站制作Google

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司