Python 爬虫:豆瓣电影Top250,包括电影导演、类型、年份、主演
2018-03-05 14:01
639 查看
结果输出到文本文件中。
import codecs import requests from bs4 import BeautifulSoup headers={'User-Agent': 'Mozilla/5.0'} index_url = 'https://movie.douban.com/top250' def get_html(url): html = requests.get(url, headers=headers).text return html def create_list(html): soup = BeautifulSoup(html, 'lxml') movie_names = [] movie_info = [] for t in soup.find_all('div', 'hd'): name = t.find('span', 'title').get_text() movie_names.append(name) for t in soup.find_all('div', 'info'): info = t.find('p').get_text().replace(' ','') movie_info.append(info) next_page = soup.find('span', 'next').find('a') if next_page: return movie_names, movie_info, index_url + next_page['href'] else: return movie_names, movie_info, None def main(): order = 1 url = index_url with codecs.open('top250.txt', 'wb', encoding='utf-8') as f: while url: html = get_html(url) names, info, url = create_list(html) for n in range(25): f.write('Top ' + str(order) + ' ' + names + '\r\n') f.write(info + '\r\n') order = order + 1 if __name__ == '__main__': main()
相关文章推荐
- python爬虫学习test1-抓取豆瓣电影 Top 250名次、影片名称、年份、导演等字段
- [python爬虫入门]爬取豆瓣电影排行榜top250
- Python爬虫初学(1)豆瓣电影top250评论数
- [Python/爬虫]利用xpath爬取豆瓣电影top250
- Python爬虫实战——豆瓣电影Top250
- Python 采用Scrapy爬虫框架爬取豆瓣电影top250
- Python爬虫初学(2)豆瓣电影top250评论数
- python爬虫 豆瓣电影top250
- [python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- Python爬虫实战——豆瓣电影top250
- (7)Python爬虫——爬取豆瓣电影Top250
- Python爬虫一:抓取豆瓣电影Top250
- 实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
- python爬虫 Scrapy2-- 爬取豆瓣电影TOP250
- 运维学python之爬虫高级篇(五)scrapy爬取豆瓣电影TOP250
- python3[爬虫基础入门实战] 爬取豆瓣电影排行top250
- Python爬虫小案例:豆瓣电影TOP250
- python 爬虫 保存豆瓣TOP250电影海报及修改名称
- 萌新的Python学习日记 - 爬虫无影 - 爬取豆瓣电影top250并入库:豆瓣电影top250
- Python爬虫——豆瓣电影Top250