设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 15945|回复: 1
打印 上一主题 下一主题

抓取微信公众账号每天的文章更新(抛出第一块砖)

[复制链接]

0

主题

0

好友

63

积分

注册会员

Rank: 2

楼主
发表于 2013-9-13 13:08:39 |显示全部楼层
不清楚"传送门"是这么抓取到微信的文章的,找不到微信的api接口
我尝试抓取传送门里面的文章,但是发现主体文章列表是ajax返回的。直接访问ajax地址是404页面,应该是判断了refer
无解...
回复

使用道具 举报

0

主题

0

好友

63

积分

注册会员

Rank: 2

沙发
发表于 2013-9-13 14:21:49 |显示全部楼层
用urllib2伪造了referer和user-agent,终于抓到了
  1. import urllib2, HTMLParser

  2. article_list = []

  3. class MyParser(HTMLParser.HTMLParser):
  4.     def __init__(self):
  5.         HTMLParser.HTMLParser.__init__(self)

  6.     def handle_starttag(self,tag,attrs):
  7.         if tag == 'a':
  8.             for name,value in attrs:
  9.                 if name == 'href':
  10.                     print(value)
  11.                     article_list.append(value)


  12. def fetch_data(uri):
  13.     request = urllib2.Request(uri)
  14.     request.add_header('Referer','http://chuansongme.com/account/crossincode')
  15.     request.add_header('Content-Type','application/x-www-form-urlencoded')
  16.     request.add_header('User-Agent','fake-client')
  17.     response = urllib2.urlopen(request)
  18.     return response



  19. list_str = fetch_data('http://chuansongme.com/more/account-crossincode/recent?lastindex=0').read()
  20. print(list_str)

  21. my = MyParser()
  22. my.feed(list_str.decode('utf-8'))

  23. article = fetch_data(article_list[0]).read()
  24. print(article)

  25. f = open('weixin.html','w')
  26. f.write(article)
  27. f.close()
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-19 00:33 , Processed in 0.023862 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部