我有一个项目,我必须刮掉50名演员/女演员的所有评级,这意味着我必须访问并刮掉3500个网页.这比预期的要长,我正在寻找一种加快速度的方法.我知道有像scrapy这样的框架,但我想在没有任何其他模块的情况下工作.是否有一种快速简便的方法来重写我的代码,或者这需要花费太多时间?我的代码如下:
def getMovieRatingDf(movie_links):
counter = -1
movie_name = []
movie_rating = []
movie_year = []
for movie in movie_links.tolist()[0]:
counter += 1
request = requests.get('http://www.imdb.com/' + movie_links.tolist()[0][counter])
film_soup = BeautifulSoup(request.text, 'html.parser')
if (film_soup.find('div', {'class': 'title_wrapper'}).find('a').text).isdigit():
movie_year.append(int(film_soup.find('div', {'class': 'title_wrapper'}).find('a').text))
# scrap the name and year of the current film
movie_name.append(list(film_soup.find('h1'))[0])
try:
movie_rating.append(float(film_soup.find('span', {'itemprop': 'ratingValue'}).text))
except AttributeError:
movie_rating.append(-1)
else:
continue
rating_df = pd.DataFrame(data={"movie name": movie_name, "movie rating": movie_rating, "movie year": movie_year})
rating_df = rating_df.sort_values(['movie rating'], ascending=False)
return rating_df
Run Code Online (Sandbox Code Playgroud)