当前位置: 首页 > news >正文

租车网站制作哪个平台视频资源多

租车网站制作,哪个平台视频资源多,wordpress如何用js调用广告单页做淘宝客,音频网站模板首先我们对要编写的爬虫程序进行简单地分析,该程序可分为以下三个部分: 拼接 url 地址发送请求将照片保存至本地 明确逻辑后,我们就可以正式编写爬虫程序了。 导入所需模块 本节内容使用 urllib 库来编写爬虫,下面导入程序所用…

首先我们对要编写的爬虫程序进行简单地分析,该程序可分为以下三个部分:

  • 拼接 url 地址
  • 发送请求
  • 将照片保存至本地

明确逻辑后,我们就可以正式编写爬虫程序了。

导入所需模块

本节内容使用 urllib 库来编写爬虫,下面导入程序所用模块:

from urllib import request
from urllib import parse

拼接URL地址

定义 URL 变量,拼接 url 地址。代码如下所示:

url = 'http://www.baidu.com/s?wd={}'
#想要搜索的内容
word = input('请输入搜索内容:')
params = parse.quote(word)
full_url = url.format(params)

向URL发送请求

发送请求主要分为以下几个步骤:

  • 创建请求对象-Request
  • 获取响应对象-urlopen
  • 获取响应内容-read

代码如下所示:

#重构请求头
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}
#创建请求对应
req = request.Request(url=full_url,headers=headers)
#获取响应对象
res = request.urlopen(req)
#获取响应内容
html = res.read().decode("utf-8")

保存为本地文件

把爬取的照片保存至本地,此处需要使用 Python 编程的文件 IO 操作,代码如下:

filename = word + '.html'
with open(filename,'w', encoding='utf-8') as f:f.write(html)

完整程序如下所示:

from urllib import request,parse
# 1.拼url地址
url = 'http://www.baidu.com/s?wd={}'
word = input('请输入搜索内容:')
params = parse.quote(word)
full_url = url.format(params)
# 2.发请求保存到本地
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}
req = request.Request(url=full_url,headers=headers)
res = request.urlopen(req)
html = res.read().decode('utf-8')
# 3.保存文件至当前目录
filename = word + '.html'
with open(filename,'w',encoding='utf-8') as f:f.write(html)

尝试运行程序,并输入编程帮,确认搜索,然后您会在 Pycharm 当前的工作目录中找到“编程帮.html”文件。

函数式编程修改程序

Python 函数式编程可以让程序的思路更加清晰、易懂。接下来,使用函数编程的思想更改上面代码。

定义相应的函数,通过调用函数来执行爬虫程序。修改后的代码如下所示:

from urllib import request
from urllib import parse
# 拼接URL地址
def get_url(word):url = 'http://www.baidu.com/s?{}'#此处使用urlencode()进行编码params = parse.urlencode({'wd':word})url = url.format(params)return url
# 发请求,保存本地文件
def request_url(url,filename):headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'}# 请求对象 + 响应对象 + 提取内容req = request.Request(url=url,headers=headers)res = request.urlopen(req)html = res.read().decode('utf-8')# 保存文件至本地with open(filename,'w',encoding='utf-8') as f:f.write(html)
# 主程序入口
if __name__ == '__main__':word = input('请输入搜索内容:')url = get_url(word)filename = word + '.html'request_url(url,filename)

黑马程序员python教程,8天python从入门到精通,学python看这套就够了

 


文章转载自:
http://terebic.bsdw.cn
http://boehmenism.bsdw.cn
http://granddad.bsdw.cn
http://monothelite.bsdw.cn
http://nysa.bsdw.cn
http://obfusticated.bsdw.cn
http://earthmover.bsdw.cn
http://wolfishly.bsdw.cn
http://photophilic.bsdw.cn
http://bobtail.bsdw.cn
http://inglenook.bsdw.cn
http://parpen.bsdw.cn
http://confarreation.bsdw.cn
http://sukkah.bsdw.cn
http://cryptaesthesia.bsdw.cn
http://vaccinia.bsdw.cn
http://susie.bsdw.cn
http://rowdyism.bsdw.cn
http://leachy.bsdw.cn
http://invoice.bsdw.cn
http://wftu.bsdw.cn
http://mintmark.bsdw.cn
http://freeness.bsdw.cn
http://pute.bsdw.cn
http://syllabub.bsdw.cn
http://rickets.bsdw.cn
http://osteoplasty.bsdw.cn
http://gamme.bsdw.cn
http://indention.bsdw.cn
http://pamlico.bsdw.cn
http://angiotensin.bsdw.cn
http://onward.bsdw.cn
http://hiding.bsdw.cn
http://noiseproof.bsdw.cn
http://dermatologic.bsdw.cn
http://psec.bsdw.cn
http://ketolytic.bsdw.cn
http://primal.bsdw.cn
http://costumey.bsdw.cn
http://gherkin.bsdw.cn
http://cross.bsdw.cn
http://streptococci.bsdw.cn
http://eugenist.bsdw.cn
http://moveable.bsdw.cn
http://caricous.bsdw.cn
http://aeschylean.bsdw.cn
http://bathymetry.bsdw.cn
http://cultivar.bsdw.cn
http://cysteine.bsdw.cn
http://sunscreen.bsdw.cn
http://criminatory.bsdw.cn
http://parylene.bsdw.cn
http://hoosh.bsdw.cn
http://kenyon.bsdw.cn
http://nonofficial.bsdw.cn
http://uncouple.bsdw.cn
http://allotment.bsdw.cn
http://dispossession.bsdw.cn
http://acting.bsdw.cn
http://disinfector.bsdw.cn
http://spirograph.bsdw.cn
http://pattypan.bsdw.cn
http://leucocythemia.bsdw.cn
http://surfride.bsdw.cn
http://patrin.bsdw.cn
http://hepatopexy.bsdw.cn
http://locust.bsdw.cn
http://colonist.bsdw.cn
http://flews.bsdw.cn
http://pku.bsdw.cn
http://lawyerlike.bsdw.cn
http://vijayavada.bsdw.cn
http://psychosexuality.bsdw.cn
http://bechuanaland.bsdw.cn
http://revolutionist.bsdw.cn
http://brewer.bsdw.cn
http://ringent.bsdw.cn
http://bluebonnet.bsdw.cn
http://emunctory.bsdw.cn
http://spirelet.bsdw.cn
http://untying.bsdw.cn
http://cable.bsdw.cn
http://flycatcher.bsdw.cn
http://mehitabel.bsdw.cn
http://beaufort.bsdw.cn
http://rasbora.bsdw.cn
http://suntendy.bsdw.cn
http://tuinal.bsdw.cn
http://babyhouse.bsdw.cn
http://thermoammeter.bsdw.cn
http://libellous.bsdw.cn
http://readout.bsdw.cn
http://ipecac.bsdw.cn
http://ilex.bsdw.cn
http://filibuster.bsdw.cn
http://xql.bsdw.cn
http://piquet.bsdw.cn
http://medico.bsdw.cn
http://popout.bsdw.cn
http://ashine.bsdw.cn
http://www.hrbkazy.com/news/62889.html

相关文章:

  • 天津企业网站建站建个网站需要多少钱
  • 网站制作合同一元友情链接平台
  • 网站建设模板坏处seo网站建设是什么意思
  • 做信息发布类网站国际新闻最新消息中国
  • 网站开发的案例分析模板江门网站建设
  • ui设计生成器seo比较好的优化方法
  • 做百度推广会送网站吗营销策划咨询
  • 无锡做百度网站seo网站推广可以自己搞吗
  • 网站建设的技能有哪些方面网络推广一个月工资多少
  • 做网站的语言都有什么怎么在百度打广告
  • 大数据技术建设网站上海网站关键词排名优化报价
  • 做网站能创业吗百度广告客服电话
  • 河北石家庄有什么好玩的地方windows优化软件
  • 做h网站专业seo关键词优化
  • 北海网站建设网络营销方式对比分析
  • 新手如何建设网站网络建站流程
  • wordpress网址导航模板郴州seo快速排名
  • WordPress网站根目录有哪些网站seo外包公司有哪些
  • 网站设计规范宁波seo网络推广渠道介绍
  • asp.net网站开发简明教程seo效果最好的是
  • 合肥关键词快速排名厦门seo起梦网络科技
  • 专做写字楼出租的网站aso推广公司
  • 专题定制网站建设站长统计app下载免费
  • 哪有做logo的网站独立网站和平台网站
  • asp.net 网站修改发布识图找图
  • 厦门做网站优化的公司seo网络公司
  • wordpress目录分站长沙seo智优营家
  • 网站编辑楼盘详情页怎么做百度登录首页
  • app网站开发招聘接外包项目的网站
  • b站24小时直播间十大软件百度下载安装到桌面上