project

一时兴起决定写写爬虫来当学生项目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import urllib.request
import re

url = "http://www.qiushibaike.com/"
tag = "hot/page/1"
headers = {
'User-Agent': 'User-Agent:Mozilla/5.0'
}
request = urllib.request.Request(url+tag, headers=headers)
response = urllib.request.urlopen(request)
content = response.read()
content_utf8 = content.decode('utf-8')

restr = '<div class="article.*?<div class="author.*?<h2>(.*?)</h2>.*?</div>.*?href="/article/(.*?)".*?<span>(.*?)</span>.*?</div>'
pattern = re.compile(restr, re.S)
items = re.findall(pattern, content_utf8)
write_html(content)

扒了糗事百科首页上的作者,链接。
目前打算是写一个自动爬段子分类推送的系统。然而发现现在的网站都坏坏,一个个的都不给人爬了。一会限制访问一会网站验证的。任重而道远啊。

更新:
爬虫计划暂停。感觉自己需要一个大项目把所有知识整合起来。一时兴起又决定写一个小的数据库。具体能完成到什么样子我也不知道。希望至少可以做成本地多线程磁盘缓存版本吧。