python 简单爬虫

ok075588 2017-11-23 04:24:37
您好,一个简单的爬虫,新手不懂,不能正确返回结果。求帮手指正,谢谢.

#coding=utf-8
import urllib
import urllib2
import requests
import cookielib
import time
import socket
html='http://www.nlfang.com/info_list.asp'

post_data={'area_city': '%C4%CF%C1%EA%CF%D8&',
'late_class': '%B7%BF%CE%DD%B3%F6%CA%DB&',
'info_key': '%D6%D0%D1%EB'}

cook={'ASPSESSIONIDSQTTDCBB': 'DJIHGCDDJDDONFJDIGFIGCLC', 'Hm_lvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469', 'ad_play_index': '29', 'Hm_lpvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469', 'Hm_lvt_239f7aa51fc37d3b008907442ed09e64': '1511423468', 'Hm_lpvt_239f7aa51fc37d3b008907442ed09e64': '1511423468'}

header={'Accept': 'text/html, application/xhtml+xml, */*',
'Referer': 'http://www.nlfang.com/',
'Accept-Language': 'zh-CN',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
'Content-Type': 'application/x-www-form-urle',
'Accept-Encoding': 'gzip, deflate',
'Host': 'www.nlfang.com',
'Content-Length': '86',
'Connection': 'Keep-Alive',
'Cache-Control': 'no-cache',
'Cookie': cook}
post_data=urllib.urlencode(post_data)
res=urllib2.Request(html,headers=header,data=post_data)
response=urllib2.urlopen(res)
print response
...全文
196 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
扶瑶直上 2017-11-28
  • 打赏
  • 举报
回复
直接requests就行了
# coding=utf-8
import requests

html = 'http://www.nlfang.com/info_list.asp'

post_data = {'area_city': '%C4%CF%C1%EA%CF%D8&',
             'late_class': '%B7%BF%CE%DD%B3%F6%CA%DB&',
             'info_key': '%D6%D0%D1%EB'}

cook = {'ASPSESSIONIDSQTTDCBB': 'DJIHGCDDJDDONFJDIGFIGCLC', 'Hm_lvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469',
        'ad_play_index': '29', 'Hm_lpvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469',
        'Hm_lvt_239f7aa51fc37d3b008907442ed09e64': '1511423468',
        'Hm_lpvt_239f7aa51fc37d3b008907442ed09e64': '1511423468'}

header = {'Accept': 'text/html, application/xhtml+xml, */*',
          'Referer': 'http://www.nlfang.com/',
          'Accept-Language': 'zh-CN',
          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
          'Content-Type': 'application/x-www-form-urle',
          'Accept-Encoding': 'gzip, deflate',
          'Host': 'www.nlfang.com',
          'Content-Length': '86',
          'Connection': 'Keep-Alive',
          'Cache-Control': 'no-cache',
          }
session = requests.session()
r = session.get(html, headers=header, data=post_data, cookies=cook)
print r.text
扶瑶直上 2017-11-28
  • 打赏
  • 举报
回复
# coding=utf-8
import requests

html = 'http://www.nlfang.com/info_list.asp'

post_data = {'area_city': '%C4%CF%C1%EA%CF%D8&',
             'late_class': '%B7%BF%CE%DD%B3%F6%CA%DB&',
             'info_key': '%D6%D0%D1%EB'}

cook = {'ASPSESSIONIDSQTTDCBB': 'DJIHGCDDJDDONFJDIGFIGCLC', 'Hm_lvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469',
        'ad_play_index': '29', 'Hm_lpvt_b1214475eca64e9e15a8bf68ccb444e4': '1511423469',
        'Hm_lvt_239f7aa51fc37d3b008907442ed09e64': '1511423468',
        'Hm_lpvt_239f7aa51fc37d3b008907442ed09e64': '1511423468'}

header = {'Accept': 'text/html, application/xhtml+xml, */*',
          'Referer': 'http://www.nlfang.com/',
          'Accept-Language': 'zh-CN',
          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko',
          'Content-Type': 'application/x-www-form-urle',
          'Accept-Encoding': 'gzip, deflate',
          'Host': 'www.nlfang.com',
          'Content-Length': '86',
          'Connection': 'Keep-Alive',
          'Cache-Control': 'no-cache',
          }
session = requests.session()
r = session.get(html, headers=header, data=post_data, cookies=cook)
print r.text
oyljerry 2017-11-23
  • 打赏
  • 举报
回复
先看看返回的结果是什么内容, 估计有什么header的参数不被服务器认可等

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧