Python爬取京东商品列表

爬取代码:

import requests
from bs4 import BeautifulSoup

def page_url(url):
    for i in range(1, 3):
        if (i % 2) == 1:
            message(url.format(i))

def message(url):
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    n = 0
    for news in soup.select('.gl-i-wrap'):
        title = news.select('.p-name')[0].text.strip()
        price = news.select('.p-price')[0].text.strip()
        commit = news.select('.p-commit')[0].text.strip()
        urls = r'http://' + news.select('.p-img')[0].contents[1]['href']
        n += 1
        print("%d、 \n 名称:%s \n 价格:%s \n 评价:%s \n 链接:%s" %  (n, title, price, commit, urls))
        f = open('info.txt', 'a+', encoding='utf-8')
        f.write(str(str(n)+title+price+commit+urls))

url = 'https://search.jd.com/Search?keyword=%E9%9E%8B%E5%AD%90&enc=utf-8&wq=%E9%9E%8B%E5%AD%90&pvid=2cb987320c55495393d8b67cce3532b3'

page_url(url)

生成词云:

import jieba
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from scipy.misc import imread


text=''
info = open('info.txt', 'r', encoding='utf-8').read()
text += ' '.join(jieba.lcut(info))
wc = WordCloud(
    width=500,
    height=500,
    margin=2,
    background_color='white',  # 设置背景颜色
    font_path='C:\Windows\Fonts\STZHONGS.TTF',  # 若是有中文的话,这句代码必须添加,不然会出现方框,不出现汉字
    max_words=2000,  # 设置最大现实的字数
    stopwords=STOPWORDS,  # 设置停用词
    max_font_size=150,  # 设置字体最大值
    random_state=42  # 设置有多少种随机生成状态,即有多少种配色方案
)
wc.generate_from_text(text)
wc.to_file('ysx.jpg')

转载于:https://www.cnblogs.com/TopHin/p/8915888.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值