抓取糗事百科文字笑话
发布时间:2020-05-30 18:16:37 所属栏目:Python 来源:互联网
导读:抓取糗事百科文字笑话
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 # -*- coding:utf-8 -*-
#制作者:archingB
import urllib
import urllib2
import re
readme="按下回车键开始读取,q退出,w写入save.txt"
def getpage(page):
url='http://www.qiushibaike.com/textnew/page/' + str(page)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
request = urllib2.Request(url,headers = headers)
response = urllib2.urlopen(request)
content = response.read().decode('utf-8')
pattern = re.compile('<div class="content">(.*?)<!--d{10}-->',re.S)
items=re.findall(pattern,content)
return items
except urllib2.URLError,e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason
def writes(item):
item_swap=item.encode('utf-8')
try:
f=open('save.txt','a')
f.write(str(M)+'n'+item_swap+'n')
finally:
if f:
f.close()
def main():
P=1
N=0
IS=True
print readme.decode('utf-8')
global M
M=1
items=getpage(P)
raw_input()
while(IS):
if(N+1==len(items)):
P+=1
N=0
items=getpage(P)
items[N]=re.sub(r'<br/>','n',items[N])
print N+1,items[N]
static=str(raw_input())
if(static=='q'or static=='Q'):
IS=False
break
if(static=='w'or static=='W'):
writes(items[N])
M+=1
N+=1
main()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
