Last active
May 14, 2016 14:53
-
-
Save LyonUp/cc10ceae37e4473db7805fadc4fd13a7 to your computer and use it in GitHub Desktop.
Web crawler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import codecs | |
DOWNLOAD_URL = 'http://book.douban.com/top250/' | |
def download_page(url): | |
data = requests.get(url).content | |
return data | |
def parse_html(html): | |
soup = BeautifulSoup(html) | |
book_list_soup = soup.find('div', attrs={'class': 'indent'}) | |
book_name_list = [] | |
for book_li in book_list_soup.find_all('table'): | |
detail = book_li.find('div', attrs={'class': 'pl2'}) | |
book_name = detail.find('a').getText(strip=True) | |
book_name_list.append(book_name) | |
next_page = soup.find('span', attrs={'class': 'next'}).find('a') | |
if next_page: | |
return book_name_list, next_page['href'] | |
return book_name_list, None | |
def main(): | |
url = DOWNLOAD_URL | |
with codecs.open('books', 'wb', encoding='utf-8') as fp: | |
while url: | |
html = download_page(url) | |
books, url = parse_html(html) | |
fp.write(u'{books}\n'.format(books='\n'.join(books))) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
import requests | |
from bs4 import BeautifulSoup | |
DOWNLOAD_URL = 'http://movie.douban.com/top250/' | |
def download_page(url): | |
data = requests.get(url).content | |
return data | |
def parse_html(html): | |
soup = BeautifulSoup(html) | |
move_list_soup = soup.find('ol', attrs={'class': 'grid_view'}) | |
movie_name_list = [] | |
for movie_li in move_list_soup.find_all('li'): | |
detail = movie_li.find('div', attrs={'class': 'hd'}) | |
movie_name = detail.find('span', attrs={'class': 'title'}).getText() | |
movie_name_list.append(movie_name) | |
next_page = soup.find('span', attrs={'class': 'next'}).find('a') | |
if next_page: | |
return movie_name_list, DOWNLOAD_URL + next_page['href'] | |
return movie_name_list, None | |
def main(): | |
url = DOWNLOAD_URL | |
with codecs.open('movies', 'wb', encoding='utf-8') as fp: | |
while url: | |
html = download_page(url) | |
movies, url = parse_html(html) | |
fp.write(u'{movies}\n'.format(movies='\n'.join(movies))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python for Web crawler