forked from queensun/Nyspider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovieinfor.py
104 lines (96 loc) · 3.22 KB
/
movieinfor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
from bs4 import BeautifulSoup
import time
import os
import chardet
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
'Host':"movie.douban.com",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"}
def search(name):
url='https://movie.douban.com/subject_search?search_text=%s&cat=1002'%name
html=requests.get(url,headers=headers).text
item=BeautifulSoup(html).find('div',{'class':'article'}).find('tr',{'class':'item'})
url=item.find('a').get('href')
return url
def movie_infor(url):
html=requests.get(url,headers=headers).text
soup=BeautifulSoup(html).find('div',id='content')
title=soup.find('h1').get_text().replace('\n','').replace('主演:','')
table=soup.find('div',id='info')
try:
actor=table.find('span',{'class':'actor'}).get_text().replace('\n','')
except:
actor='-'
infors=str(table).split('<br/>')
date='-'
runtime='-'
movie_types=[]
for item in infors:
if '上映日期:' in item:
date=BeautifulSoup(item).get_text().replace('上映日期:','').replace('\n','')
if '片长:' in item:
runtime=BeautifulSoup(item).get_text().replace('片长:','').replace('\n','')
if '类型:' in item:
movie_types=BeautifulSoup(item).get_text().replace('类型:','').replace('\n','').replace(' ','').split('/')
try:
des=soup.find('div',id='link-report').get_text().replace(' ','').replace('\n','')
except:
des='-'
try:
rate=soup.find('strong',{'class':['ll','rating_num']}).get_text()
except:
rate='-'
try:
rating_betterthan=soup.find('div',id='interest_sectl').find('div',{'class':'rating_betterthan'}).find_all('a')
except:
rating_betterthan=[]
line=''
for mtype in movie_types:
line+='/'+mtype
for a in rating_betterthan:
if mtype in str(a):
try:
num=int(a.get_text().split('%')[0])
num=str(num/10)
except:
num=''
line+=' '+num
line=line[1:]
template='(\r\n`%s`,\r\n`%s`,\r\n`%s`,\r\n`%s`,\r\n`%s`,\r\n`%s`,\r\n`%s`\r\n),\r\n'
text=template%(title,des,actor,runtime,rate,date,line)
return text
def get_chardet(filename):
data=open(filename,'rb').read()
coding=chardet.detect(data)['encoding']
if coding=='GB2312':
coding='GBK'
return coding
def load_names():
names=[]
encoding=get_chardet('movie_name.txt')
for line in open('movie_name.txt','r',encoding=encoding):
line=line.replace('\r','').replace('\n','')
names.append(line)
return names
def main():
names=load_names()
for name in names:
try:
url=search(name)
except:
print(name,'failed')
continue
try:
text=movie_infor(url)
except:
print(name,'failed')
continue
f=open('data.txt','a',encoding='utf-8')
f.write(text)
f.close()
print(name,'ok')
time.sleep(2)
main()