Python, Crawler and Raspberry Pi

I left RP for quite a while as I did not think I can achieve something with RP in a short run. But since I’ve started Python, I start Raspberry Pi.

Today, at the moment, I’m fabricating another piece of crap to introduce the new web crawler. BeautifulSoup and mechanize.
It’s only that I’m glad I start to learn about Python. Emphasis on About…

from bs4 import BeautifulSoup
import mechanize
import time
import urllib
import string

start = "http://" +raw_input ("Where would you like to start searching?\n")
br = mechanize.Browser()
r = br.open(start)
html = r.read() # It's the moment xml gets generated.

soup = BeautifulSoup(html)
for link in soup.find_all('a'):
	print(link.get('href')) #This prints out all sub directories.

why cannot see the pic

Full code from “Learn Raspberry Pi Programming with Python”, see the note, that is important.

import mechanize
import time
from bs4 import BeautifulSoup
import re
import string
import os
import urllib

def downloadProcess(html,base,filetype,linkList):
	"This does the actual file downloading."
	soup = BeautifulSoup(html)
	for link in soup.find_all('a'):
		linkText = str(link.get('href')) # here we assume the href starts at sub directory, not start with http://
		
		if filetype in linkText:
			slashList = [i for i, ind in enumerate(linkText) if ind == '/'] # it can store the index of "/"
			directoryName = linkText[(slashList[0]+1):slashList[1]]  # Slice to get the name of Directory
			if not os.path.exists(directoryName):
				os.makedirs(directoryName)
			
			image = urllib.URLopener()
			linkGet = base + linkText
			filesave = string.lstrip(linkText,"/")
			image.retrieve(linkGet,filesave)

		elif "htm" in linkText: #cover both html and htm
			linkList.append(link)
			

start = "http://" + raw_input("Where would you like to start searching?\n")
filetype = raw_input("What file type are you looking for?\n")

numSlash = start.count('/')
slashList = [i for i, ind in enumerate(start) if ind == '/']

if (len(slashList) >= 3): 
	third = slashList[2]
	base = start[:third]

else:
	base = start

br = mechanize.Browser()
r = br.open(start)
html = r.read()
linkList = []

print "Parsing" + start
downloadProcess(html,base,filetype,linkList)

for leftover in linkList:

	time.sleep(0.1)
	linkText = str(leftover.get('href'))
	print "Parsing" + base + linkText
	br = mechanize.Browser()
	r = br.open(base + linkText)
	html = r.read()
	linkList = []
	downloadProcess(html,base,filetype,linkList) # The recursion: depth goes first.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值