爬虫练习

Posted lifengwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫练习相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib.request#这里必须要加request
import urllib.parse
import requests
import sys
#sys.Setdefaultencoding(‘utf-8‘)
import urllib
import json
import time
from bs4 import BeautifulSoup
#发起GET请求
# url = ‘http://kaoshi.edu.sina.com.cn/college/scorelist?tab=batch&wl=1&local=2&batch=&syear=2013‘
# response = urllib.request.urlopen(url=url)
# result = response.read().decode(‘utf-8‘)#解码后可以正常输出
# print(result)
#发起POST请求
url = "http://shuju.wdzj.com/plat-info-59.html"
data = urllib.parse.urlencode({type1:x,type2:0,status:0}).encode(utf-8)
request = urllib.request.Request(url=url,data=data)
#opener = urllib.build_open(urllib.HTTPCookieProcessor()) #跟上述差不多,只是了一个data
response = urllib.request.urlopen(request)
result = response.read().decode(utf-8)
print(result)
result = result.replace(<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">, ‘‘)
result = result.replace(</pre></body></html>, ‘‘)
for key in json.loads(result,strict=False).keys():
    print(key)
#报错:json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)

 

以上是关于爬虫练习的主要内容,如果未能解决你的问题,请参考以下文章

scrapy主动退出爬虫的代码片段(python3)

全网最有效爬虫练习,练习完可就业(源代码交流)

爬虫日常练习-艾图网单页面图片爬取

Python爬虫练习:爬取美团网成都地区的酒店信息

第二关:爬虫HTML网页基础(附练习题)

Python爬虫练习(拉勾网北京地区数据挖掘类职位所需技能统计)