1. 程式人生 > >Python爬取京東商品列表

Python爬取京東商品列表

+= 圖片 info sta HR earch tex new html

爬取代碼:

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 = rhttp:// + 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)

技術分享圖片

Python爬取京東商品列表