用Python代码爬取电影
时间: 2023-04-01 07:04:13 浏览: 82
我可以提供一个示例代码,这样你就可以用Python代码爬取电影了:import requests, lxml.html as lhfrom lxml.cssselect import CSSSelectorimport pandas as pd#URL of the page you want to scrapurl='https://www.imdb.com/chart/top'#Create a handle, page, to handle the contents of the websitepage = requests.get(url)#Store the contents of the website under docdoc = lh.fromstring(page.content)#Parse data that are stored between <tr>..</tr> of HTMLtr_elements = doc.xpath('//tr')#Check the length of the first 12 rows[len(T) for T in tr_elements[:12]]#Create empty listcol=[]i=0#For each row, store each first element (header) and an empty listfor t in tr_elements[0]:i+=1name=t.text_content()print ('%d:"%s"'%(i,name))col.append((name,[]))#Since out first row is the header, data is stored on the second row onwardsfor j in range(1,len(tr_elements)):#T is our j'th rowT=tr_elements[j]#If row is not of size 10, the //tr data is not from our tableif len(T)!=3:break#i is the index of our columni=0#Iterate through each element of the rowfor t in T.iterchildren():data=t.text_content()#Check if row is emptyif i>0:#Convert any numerical value to integerstry:data=int(data)except:pass#Append the data to the empty list of the i'th columncol[i][1].append(data)#Increment i for the next columni+=1[len(C) for (title,C) in col]#Create the dictionary{title:column for (title,column) in col}#Create the dataframedf=pd.DataFrame(dict)#View the top 5 rowsdf.head()