当前位置:   article > 正文

python爬虫代码_网络爬虫python代码

网络爬虫python代码

python爬虫主要需要urllib

方法1

  1. import urllib.parse,urllib.request
  2. import ssl ssl._create_default_https_context = ssl._create_unverified_context
  3. req = urllib.request.Request(url = url,headers = headers)
  4. response = urllib.request.urlopen(req)
  5. return response.read().decode(coding)

方法2

  1. #导入包
  2. import urllib.request
  3. #函数
  4. def main():
  5.     preservation()
  6. def gethtml_http(url):
  7.     try:
  8.         response = urllib.request.urlopen(url,timeout = 5)
  9.         htmlfile = response.read().decode("utf-8")
  10.     except urllib.error.URLError as e:
  11.         print("超时")
  12.     return htmlfile
  13. def preservation():
  14.     h = gethtml_http("http://www.baidu.com")
  15.     print(h)
  16.     #import os
  17.     #os.rename("内部储存\hhh.py\baidu.txt","内部储存\hhh.py\baidu.html")
  18. if __name__ == "__main__":
  19.     main()

    

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/756088
推荐阅读
相关标签
  

闽ICP备14008679号