爬虫代码错误请教
import urllib.requestimport chardet
def main():
i = 0
with open("urls.txt", "r") as f:
# 读取待访问的网址
# 由于urls.txt每一行一个URL
# 所以按换行符'\n'分割
urls = f.read().splitlines()
for each_url in urls:
response = urllib.request.urlopen(each_url)
html = response.read()
# 识别网页编码
encode = chardet.detect(html)['encoding']
if encode == 'GB2312':
encode = 'GBK'
i += 1
filename = "url_%d.txt" % i
with open(filename, "w", encoding=encode) as each_file:
each_file.write(html.decode(encode, "ignore"))
if __name__ == "__main__":
main()
小甲鱼的源代码,运行出错,不知道哪里有问题(我两个文件都放在一个目录里了)
不过就算出错了还是新建了两个文本文件
不是所有网站都不设防,而是很少网站不设防
页:
[1]