使用Urllib爬虫(1)--简单的将数据爬到内存或硬盘中

  • 将数据爬取到内存中
    • import urllib
      import urllib.request
      import re
      #打开京东网页并且进行读取,解码格式utf-8,ignore小细节自动略过,大大减少出错率
      #将数据爬到内存中
      #http://www.jd.com
      url = "http://www.jd.com"
      data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
      pat = "<title>(.*?)</title>"
      #re.S模式修正符,网页数据往往是多行的,避免多行的影响
      print(re.compile(pat,re.S).findall(data))
  • 将数据爬取到硬盘中
    • import urllib
      import urllib.request
      import re
      url = "http://www.jd.com"
      #urlretrieve(网址,文件名filename),由于\有转义的作用所以改用为/或者\\
      res = urllib.request.urlretrieve(url,filename="D:\\pythonstudy\\pachong\\jd1.html")
      print(res)

猜你喜欢

转载自www.cnblogs.com/u-damowang1/p/12724139.html