Python
- '123{}678'.format('45')
-
from lxml import html
xpath_dom = html.fromString()
ul_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li')
- 三目運(yùn)算
trueStatement if condition else falseStatement
-
pandas
存儲(chǔ)為csv文件:
df = pd.DataFrame(book_list)
df.to_csv('dangdang.csv')
- iris.data[:,:2]
切片鲜滩,直接切列
import requests
from lxml import html
from wordcloud import WordCloud
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def spider_movie(address):
movie_list = []
url = 'https://movie.douban.com/cinema/later/{}'.format(address)
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
resp = requests.get(url, headers=headers)
html_data = resp.text
selector = html.fromstring(html_data)
div_list = selector.xpath('//div[@id="showing-soon"]/div')
print('共有{}部電影即將上映'.format(len(div_list)))
for div in div_list:
# 電影名
name = div.xpath('./div[@class="intro"]/h3/a/text()')[0]
# print(name)
# 上映日期
day = div.xpath('./div[@class="intro"]/ul/li/text()')[0]
# print(day)
# 類型
type = div.xpath('./div[@class="intro"]/ul/li/text()')[1]
# print(type)
# 上映國家
country = div.xpath('./div[@class="intro"]/ul/li/text()')[2]
# print(country)
# 想看人數(shù)
div_three = div.xpath('./div[@class="intro"]/ul/li')[3]
number = div_three.xpath('./span/text()')[0]
number = str(number).replace('人想看', '')
number = int(number)
# print(number)
# 添加電影信息
movie_list.append({
'name':name,
'day':day,
'type':type,
'country':country,
'number':number
})
# 排序
movie_list.sort(key=lambda x:x['number'], reverse=True)
# 遍歷
for movie in movie_list:
print(movie)
# 繪制即將上映電影最想看前五人數(shù)占比圖
top5_movie = [movie_list[i] for i in range(4)]
labels = [x['name'] for x in top5_movie]
# print(labels)
counts = [x['number'] for x in top5_movie]
# print(counts)
colors = ['red', 'purple', 'yellow', 'gray', 'green']
plt.pie(counts, labels=labels, autopct='%1.2f%%', colors=colors)
plt.legend(loc=2)
plt.axis('equal')
plt.show()
# 繪制即將上映電影國家的占比圖
total = [x['country'] for x in movie_list]
text = ''.join(total)
print(text)
words_list = jieba.lcut(text)
print(words_list)
counts = {}
excludes ={"大陸"}
for word in words_list:
if len(word) <= 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
print(counts)
for word in excludes:
del counts[word]
items = list(counts.items())
print(items)
items.sort(key=lambda x: x[1], reverse=True)
print(items)
numm = [] # 數(shù)量
labels = [] # 國家
for i in range(len(items)):
x, y = items[i]
numm.append(y)
if(x == "中國"):
x = "中國大陸"
labels.append(x)
plt.pie(numm, labels=labels, autopct='%1.2f%%')
plt.legend(loc=2)
plt.axis('equal')
plt.show()
# top5.png
text = ' '.join(labels)
WordCloud(
font_path='MSYH.TTC',
background_color='white',
width=800,
height=600,
collocations=False
).generate(text).to_file('top5.png')
spider_movie('chongqing')
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者