def scrap_section(self, section): html = urllib.urlopen(section.url).read() html = html[html.index('<html'):] soup = BeautifulSoup(html, from_encoding='latin-1') for elem in soup.ul('li'): url = elem.a.get('href') print url program_source = scrapper.models.ProgramSource.objects.get_or_create(section=section, url=url)[0] try: name = url[url.index('mira')+4:url.index('.com')] program = library.models.Program.objects.get_or_create(channel=section.channel, name=name)[0] program_source.program = program program_source.name = program_source.name or name program_source.save() program.thumbnail = program.thumbnail or urllib.basejoin(elem.a.get('href'),elem.img.get('src')) program.save() except: pass
def whitespace(): """ <p> <a href="/Whitespace" title="Whitespace">Whitespace</a> (0x20) is a <a href="/Documentation" title="Documentation">hackerspace</a> in the wonderful city of Ghent, Belgium. It is a physical space run by a group of people dedicated to various aspects of constructive & creative hacking. Our <a href="/FAQ" title="FAQ">FAQ</a> is an ever growing useful resource of information about who we are, what we do and how you can become a part of all the <b>awesomeness.</b> Also check out the hackerspaces in <a class="external text" href="http://we.voidwarranties.be">Antwerp</a>, <a class="external text" href="http://hackerspace.be/">Brussels</a> and <a class="external text" href="http://www.wolfplex.org/">Charleroi</a>. </p> """ soup = BeautifulSoup(requests.get("http://www.0x20.be/Main_Page").content) for event in soup.ul('li'): if event.text == 'More...': continue title = event.a.text url = "http://www.0x20.be" + event.a["href"] if "-" in event.b.text[:-1]: start, end = map(lambda x: parse(x.strip()), event.b.text[:-1].split("-")) else: start = parse(event.b.text[:-1]) end = None location = event('a')[1].text yield { 'title': title, 'url': url, 'start': start, 'end': end, 'location': location.strip() if location else None, 'tags': ('hackerspace',) }
# findNext(), findPrevious(), fetchNext(), fetchPrevious(), findParent(), # fetchParent() 와 같은 함수가 있다. fetch*/*Text() 함수는 테그가 아닌 테그 안의 # 문자를 찾거나 가져오고, *Next*/*Previous*/*Parent() 함수는 현재 테그에서 # 계층구조상 아래로 내려가지 않고 대신 앞뒤 혹은 위로 이동하며 조건에 맞는 테그를 # 찾는다. 각 함수의 자세한 정보는 설명서를 참고하라. def need_thumbnail(x): # 가로나 세로가 60 보다 큰 img 테그라면 True, 아니면 False if x.name == 'img': return x.get('height', 0) > 60 or x.get('width', 0) > 60 return False print soup.ul(need_thumbnail) # = soup.ul.fetch(need_thumbnail) print soup.p.findNextSibling('p') # 두번째 p 테그 # 다음과 같이 HTML 소스를 수정할 수도 있다. 단, 이때는 앞에서 본 string 같은 # 축약형을 사용할 수 없고 contents 목록을 직접 수정해야 한다. 그후 prettify() # 함수로 수정한 HTML 소스를 계층구조에 따라 들여쓰기하여 출력한다. print soup soup.title.contents[0] = '제목 수정' soup.p['class'] = 'menu' soup('p')[1].contents = ['두번째 단락 생략',] del soup.body.contents[5] print soup.prettify()
# 계층구조상 아래로 내려가지 않고 대신 앞뒤 혹은 위로 이동하며 조건에 맞는 테그를 # 찾는다. 각 함수의 자세한 정보는 설명서를 참고하라. def need_thumbnail(x): # 가로나 세로가 60 보다 큰 img 테그라면 True, 아니면 False if x.name == 'img': return x.get('height', 0) > 60 or x.get('width', 0) > 60 return False f.write(str(soup.ul(need_thumbnail))) # = soup.ul.fetch(need_thumbnail) f.write(str(soup.p.findNextSibling('p'))) # 두번째 p 테그 # 다음과 같이 HTML 소스를 수정할 수도 있다. 단, 이때는 앞에서 본 string 같은 # 축약형을 사용할 수 없고 contents 목록을 직접 수정해야 한다. 그후 prettify() # 함수로 수정한 HTML 소스를 계층구조에 따라 들여쓰기하여 출력한다. f.write(str(soup)) soup.title.contents[0] = '제목 수정' soup.p['class'] = 'menu' soup('p')[1].contents = [ '두번째 단락 생략', ] del soup.body.contents[5] f.write(str(soup.prettify()))
# fetchParent() 와 같은 함수가 있다. fetch*/*Text() 함수는 테그가 아닌 테그 안의 # 문자를 찾거나 가져오고, *Next*/*Previous*/*Parent() 함수는 현재 테그에서 # 계층구조상 아래로 내려가지 않고 대신 앞뒤 혹은 위로 이동하며 조건에 맞는 테그를 # 찾는다. 각 함수의 자세한 정보는 설명서를 참고하라. def need_thumbnail(x): # 가로나 세로가 60 보다 큰 img 테그라면 True, 아니면 False if x.name == 'img': return x.get('height', 0) > 60 or x.get('width', 0) > 60 return False f.write(str(soup.ul(need_thumbnail))) # = soup.ul.fetch(need_thumbnail) f.write(str(soup.p.findNextSibling('p'))) # 두번째 p 테그 # 다음과 같이 HTML 소스를 수정할 수도 있다. 단, 이때는 앞에서 본 string 같은 # 축약형을 사용할 수 없고 contents 목록을 직접 수정해야 한다. 그후 prettify() # 함수로 수정한 HTML 소스를 계층구조에 따라 들여쓰기하여 출력한다. f.write(str(soup)) soup.title.contents[0] = '제목 수정' soup.p['class'] = 'menu' soup('p')[1].contents = ['두번째 단락 생략',] del soup.body.contents[5] f.write(str(soup.prettify())) f.close()
# 계층구조상 아래로 내려가지 않고 대신 앞뒤 혹은 위로 이동하며 조건에 맞는 테그를 # 찾는다. 각 함수의 자세한 정보는 설명서를 참고하라. def need_thumbnail(x): # 가로나 세로가 60 보다 큰 img 테그라면 True, 아니면 False if x.name == 'img': return x.get('height', 0) > 60 or x.get('width', 0) > 60 return False print soup.ul(need_thumbnail) # = soup.ul.fetch(need_thumbnail) print soup.p.findNextSibling('p') # 두번째 p 테그 # 다음과 같이 HTML 소스를 수정할 수도 있다. 단, 이때는 앞에서 본 string 같은 # 축약형을 사용할 수 없고 contents 목록을 직접 수정해야 한다. 그후 prettify() # 함수로 수정한 HTML 소스를 계층구조에 따라 들여쓰기하여 출력한다. print soup soup.title.contents[0] = '제목 수정' soup.p['class'] = 'menu' soup('p')[1].contents = [ '두번째 단락 생략', ] del soup.body.contents[5] print soup.prettify()