在scraper的不同函数中,我使用相同的变量名
ix
对于
enumerate
. 例如:
def func():
for ix, i in range(5):
print(ix, i)
但是,这会导致一个错误。
名称错误:未定义名称“ix”
这是刮刀的代码,你可以清楚地看到
九
已定义。见
<<<<<<<
在代码中查找错误
import bs4 as bs
from urllib.request import Request, urlopen
import numpy as np
website = "https://www.rottentomatoes.com"
alphabet = 'uywz' # do not include x
def fetch(page, addition=''):
"""Fetches HTML data"""
headers = {'User-Agent': 'Opera/9.80 (X11; Linux i686; Ub'
'untu/14.10) Presto/2.12.388 Version/12.16'}
req = Request(page + addition, headers=headers)
open_request = urlopen(req).read()
soup = bs.BeautifulSoup(open_request, 'lxml')
return soup
def critics_letters(letters):
"""Creates URL for 26 pages of critics, based on the first letter of their name"""
letters_url = list()
for elem in letters:
letters_url.append("/critics/authors?letter=" + elem)
return letters_url
def critics_list(catalog):
"""Fetches the url of all listed critics"""
critics_url = list()
for ix, letter_pages in enumerate(catalog, 1):
for p in fetch(website, letter_pages).find_all("p", {"class": "critic-names"}):
for a in p.find_all("a"):
href_critic = a['href']
if str(href_critic)[:7] != "/source":
critics_url.append(href_critic + "/movies")
print('\r1/4 â {:.2%} of movie critic URLs scraped.'.format(ix/len(catalog)), end=' ')
print('\r{} pages of movie critic URLs successfully scraped.'.format(ix), end=' '); print()
return critics_url
def movies(catalog):
"""Fetches the url of the movies reviewed by the critic"""
movies_url = list()
errors = 0
for ix, critic_profile in enumerate(catalog, 1): # ASSIGNMENT HERE <<<<<<<<<<<<<<<<<
try:
checker = fetch(website, critic_profile).find_all("h2", {"class": "panel-heading js-review-type"})
if len(checker) > 0:
if checker[0].text == "Movie Reviews Only":
for td in fetch(website, critic_profile).find_all("td",
{"class": "col-xs-12 col-sm-6 critic-review-table__title-column"}):
for a in td.find_all("a"):
if a['href'] not in movies_url:
movies_url.append(a['href'])
except:
errors += 1
print('\r2/4 â {:.2%} of movie URLs scraped. Error rate: {:.2%}'.format(ix/len(catalog),
errors/ix), end=' ')
print('\r{} movie URLs successfully scraped. Error rate: {:.2%}'.format(
len(movies_url)-errors, errors/ix), end='\n') # ERROR RAISE HERE <<<<<<<<<<<<<<<<<<<<<
return movies_url
if __name__ == '__main__':
critic_main = critics_letters(alphabet)
list_critics = critics_list(critic_main)
all_movies = movies(list_critics)
为什么在第一次使用
九
? 我做了一些研究,我可以通过使用
global ix
之前
列举
,但这是个好主意吗?
请参见此处的完整代码:
scraping all critic reviews from rotten tomatoes
.