楼主: 小马哥_M
4734 25

[网帖精选] 我出来抛砖引玉一下,希望能让更多人爱上Python! [推广有奖]

11
zhangb02 发表于 2014-8-7 14:57:49
的确很方便,正在入门学习。您的代码能方便共享一下吗?

12
qdzhxg 发表于 2014-8-7 15:05:14
正在学习中,更有信息了

13
小马哥_M 发表于 2014-8-7 15:32:06
[quote]zhangb02 发表于 2014-8-7 14:57 http://my.oschina.net/mayepythoner/blog/298503

这是爬虫的代码,爬的是这个页面 http://jasss.soc.surrey.ac.uk/index_by_issue.html :
  1. #!/usr/bin/python
  2. #coding:utf-8

  3. import urllib
  4. import sys
  5. from BeautifulSoup import BeautifulSoup
  6. from HTMLParser import HTMLParser

  7. reload(sys)
  8. sys.setdefaultencoding('utf-8')
  9. #打开主网页
  10. for i in xrange(15,17):
  11.         for j in xrange(1,5):
  12.                 while True:
  13.                         url = "http://jasss.soc.surrey.ac.uk/" + str(i) + "/" + str(j) + "/contents.html"
  14.                         f = urllib.urlopen(url)
  15.                         soup = BeautifulSoup(f.read())
  16.                         blockquote = soup.find('blockquote')
  17.                         volume = blockquote.find('h1').text
  18.                         date = blockquote.find('h2').text
  19.                         articles = blockquote.findAll('a')
  20.                         if articles:
  21.                                 break
  22.                 print volume
  23.                 print date
  24.                 print '\n'

  25.                 for article in articles:
  26.                         article_url = article['href']
  27.                         while True:
  28.                                 f_article = urllib.urlopen(article_url)
  29.                                 soup_article = BeautifulSoup(f_article.read())
  30.                                 if soup_article:
  31.                                         break
  32.                         titlehtml = soup_article.find(attrs={"class":"arttitle"})
  33.                         if not titlehtml:
  34.                                 continue
  35.                         title = titlehtml.text
  36.                         authors = soup_article.find(attrs={"class":"artauthor"}).find('a').text
  37.                         contents = soup_article.find(attrs={"class":"article"}).find('dl').findAll('dd')
  38.                         abstract = contents[0].text
  39.                         keywords = contents[1].text
  40.                         print title
  41.                         print "作者:" + authors
  42.                         print "摘要:" + abstract
  43.                         print "关键词:" + keywords
  44.                         print "url:" + article_url
  45.                         print '\n'

  46. for j in xrange(1,3):
  47.         while True:
  48.                 url = "http://jasss.soc.surrey.ac.uk/17/" + str(j) + "/contents.html"
  49.                 f = urllib.urlopen(url)
  50.                 soup = BeautifulSoup(f.read())
  51.                 blockquote = soup.find('blockquote')
  52.                 volume = blockquote.find('h1').text
  53.                 date = blockquote.find('h2').text
  54.                 articles = blockquote.findAll('a')
  55.                 if articles:
  56.                         break
  57.         print volume
  58.         print date
  59.         print '\n'

  60.         for article in articles:
  61.                 article_url = article['href']
  62.                 while True:
  63.                         f_article = urllib.urlopen(article_url)
  64.                         soup_article = BeautifulSoup(f_article.read())
  65.                         if soup_article:
  66.                                 break
  67.                 titlehtml = soup_article.find(attrs={"class":"arttitle"})
  68.                 if not titlehtml:
  69.                         continue
  70.                 title = titlehtml.text
  71.                 authors = soup_article.find(attrs={"class":"artauthor"}).find('a').text
  72.                 contents = soup_article.find(attrs={"class":"article"}).find('dl').findAll('dd')
  73.                 abstract = contents[0].text
  74.                 keywords = contents[1].text
  75.                 print title
  76.                 print "作者:" + authors
  77.                 print "摘要:" + abstract
  78.                 print "关键词:" + keywords
  79.                 print "url:" + article_url
  80.                 print '\n'
  81.                                                                                 
复制代码
至于我实验的,源码价值不大,那个图主要用到一个networkx的包,它是基于matplotlib的,感兴趣的话可以看看。

14
zhangb02 发表于 2014-8-7 15:35:54
小马哥_M 发表于 2014-8-7 15:32
zhangb02 发表于 2014-8-7 14:57 http://my.oschina.net/mayepythoner/blog/298503

这是爬虫的代 ...
非常感谢!

15
firstconfig 发表于 2014-8-7 15:39:00
小马哥_M 发表于 2014-8-7 15:32
zhangb02 发表于 2014-8-7 14:57 http://my.oschina.net/mayepythoner/blog/298503

这是爬虫的代 ...
人生苦短,我用Python

16
小马哥_M 发表于 2014-8-7 15:40:44
zhangb02 发表于 2014-8-7 15:35
非常感谢!
刚才回帖不知道怎么让我改坏了~

简易浏览器的详细制作说明及源码在我的博客里:http://my.oschina.net/mayepythoner/blog/298503

感兴趣的话可以看下,哪里写的比较详细~

17
huangfanchang 发表于 2014-8-14 00:24:26
ding
kankan

18
gxhg 发表于 2014-8-14 06:47:17
我也正在学python,大家互相鼓励一下。

19
狂热的爱好者 学生认证  发表于 2014-8-14 14:33:17
鼓励在此分享~~~

20
shenliang_111 发表于 2014-8-14 17:34:10
小马哥_M 发表于 2014-8-7 13:10
再推荐你个网站吧

开源中国:http://www.oschina.net/project
楼主的画图很nb,也想做个类似的简易浏览器,有包推荐吗? 楼主用什么包做的?flask? 本人也是统计学专业的,无任何web编程经验,望点拨..谢谢

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 06:25