racoon
今天尝试爬取国家税务总局网站
from urllib import request
base_url = "http://www.chinatax.gov.cn/chinatax/n810219/n810724/index.html"
f = request.urlopen(base_url)

用上面这段代码,结果会报错:

urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.

找了一下原因,也没太看懂,大概是 因为没有cookies,而网站需要cookies, 问题在这里:
后来在别的地方有人说,可以用requests这个库来抓取信息就不会,于是用了这个库
import requests
res = requests.get("http://www.chinatax.gov.cn/chinatax/n810219/n810724/index.html")
print(res.text)

 

写了上面的代码,可以抓取了,但是又遇到一个新问题,就是抓取网页里的中文是乱码
然后又在网上寻找办法,试了很多方法,但最终解决问题很简单
 
import requests
res = requests.get("http://www.chinatax.gov.cn/chinatax/n810219/n810724/index.html")
res.encoding= \'utf-8\'     # 指定res的编码
print(res.text)

最终,问题解决。

分类:

技术点:

相关文章:

  • 2021-06-10
  • 2021-12-25
  • 2021-05-30
  • 2021-09-17
  • 2021-06-19
  • 2021-12-08
  • 2021-10-19
猜你喜欢
  • 2021-07-13
  • 2021-09-07
  • 2021-12-25
  • 2021-11-23
  • 2021-12-25
  • 2021-08-27
相关资源
相似解决方案