Python爬虫類、BeautifulSoupを使用して豆弁映画TOP 250映画情報(BeautifulSoup,lxml)を爬虫類
1972 ワード
:Python , TOP250 ,https://blog.csdn.net/licx1988/article/details/102869923
, BeautifulSoup ,
:BeautifulSoup
:lxml
:find() find_all()
BeautifulSoup :https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
:requests,BeautifulSoup, lxml ,find(), find_all() 。
: TOP250 。
# lxml ,find(), find_all() , TOP250
import requests
from bs4 import BeautifulSoup
from requests.exceptions import RequestException
import time
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
}
def get_page(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
except RequestException:
print(" ……")
def parse_page(html):
soup = BeautifulSoup(html, 'lxml')
for div in soup.find_all(attrs={'class': 'item'}): # , class item div ;
yield {
'name': div.find('img').get('alt'), # , img , alt
'rank': div.find('em').string, # , em ;
'score': div.find(class_='rating_num').string, # , class rating_num span , span
'movie_url': div.find('a').get('href'), # , a , href
'movie_pic_url': div.find('img').get('src') # , img , src
}
def main(start):
url = 'https://movie.douban.com/top250?start=' + str(start)
html = get_page(url)
for item in parse_page(html):
print(item)
if __name__ == '__main__':
for i in range(10):
main(i*25)
time.sleep(2)