1. 程式人生 > >Python爬取淘寶頁面的資料,包含商品名字,價格及地址

Python爬取淘寶頁面的資料,包含商品名字,價格及地址

作業系統:Windows7專業版

Python版本:3.6.4

ide:PyCharm Community Edition 4.0.4

程式碼如下:

# -*- coding:utf-8 -*-
__author__ = 'zengqiang.wang'
import re
import requests

"""
思想來源:閒來無事,就想著能不能抓取點淘寶上的資料來玩玩呢?
於是在百度搜索了下,下面的程式碼就是參考這位仁兄的基礎上做的。
在這裡表示感謝
地址如下:
http://blog.csdn.net/haha1fan/article/details/54911962

需求:
抓取100個搜尋關鍵字為Head First Python的淘寶結果頁面,
把結果中的商品資訊儲存到txt檔案中。
"""
url = 'https://s.taobao.com/search'
payload = {'q': 'Head First Python','s': '1','ie':'utf8'}  #字典傳遞url引數
file = open('taobao_product.txt','w',encoding='utf-8')

for k in range(0,100):        #100次,就是100個頁的商品資料

    payload ['s'] = 44*k+1   #此處改變的url引數為s,s為1時第一頁,s為45是第二頁,89時第三頁以此類推
    resp = requests.get(url, params = payload)
    print(resp.url)          #列印訪問的網址
    resp.encoding = 'utf-8'  #設定編碼
    title = re.findall(r'"raw_title":"([^"]+)"',resp.text,re.I)  #正則儲存所有raw_title的內容,這個是書名,下面是價格,地址
    price = re.findall(r'"view_price":"([^"]+)"',resp.text,re.I)
    loc = re.findall(r'"item_loc":"([^"]+)"',resp.text,re.I)
    x = len(title)           #每一頁商品的數量

    for i in range(0,x) :    #把列表的資料儲存到檔案中
        file.write(str(k*44+i+1)+'書名:'+title[i]+'\n'+'價格:'+price[i]+'\n'+'地址:'+loc[i]+'\n\n')


file.close()
注意:

1.在python3.6.4下下載request庫:

在dos下輸出結果如下

Microsoft Windows [版本 6.1.7601]
版權所有 (c) 2009 Microsoft Corporation。保留所有權利。

C:\Users\zengqiang.wang>D:

D:\>cd Python

D:\Python>dir
 驅動器 D 中的卷沒有標籤。
 卷的序列號是 FA1B-12CC

 D:\Python 的目錄

2017/12/26  18:39    <DIR>          .
2017/12/26  18:39    <DIR>          ..
2017/12/26  18:40    <DIR>          Python36
               0 個檔案              0 位元組
               3 個目錄 76,675,727,360 可用位元組

D:\Python>cd Python36

D:\Python\Python36>dir
 驅動器 D 中的卷沒有標籤。
 卷的序列號是 FA1B-12CC

 D:\Python\Python36 的目錄

2017/12/26  18:40    <DIR>          .
2017/12/26  18:40    <DIR>          ..
2017/12/26  18:40    <DIR>          DLLs
2017/12/26  18:39    <DIR>          Doc
2017/12/26  18:39    <DIR>          include
2017/12/26  18:40    <DIR>          Lib
2017/12/26  18:40    <DIR>          libs
2017/12/19  07:01            30,334 LICENSE.txt
2017/12/19  07:01           379,756 NEWS.txt
2017/12/19  06:58           100,504 python.exe
2017/12/19  06:55            58,520 python3.dll
2017/12/19  06:55         3,610,776 python36.dll
2017/12/19  06:58            98,968 pythonw.exe
2017/12/26  18:40    <DIR>          Scripts
2017/12/26  18:40    <DIR>          tcl
2017/12/26  18:40    <DIR>          Tools
2016/06/09  22:53            87,888 vcruntime140.dll
               7 個檔案      4,366,746 位元組
              10 個目錄 76,675,727,360 可用位元組

D:\Python\Python36>cd Lib

D:\Python\Python36\Lib>cd ..

D:\Python\Python36>cd Scripts

D:\Python\Python36\Scripts>dir
 驅動器 D 中的卷沒有標籤。
 卷的序列號是 FA1B-12CC

 D:\Python\Python36\Scripts 的目錄

2017/12/26  18:40    <DIR>          .
2017/12/26  18:40    <DIR>          ..
2017/12/26  18:40            98,160 easy_install-3.6.exe
2017/12/26  18:40            98,160 easy_install.exe
2017/12/26  18:40            98,132 pip.exe
2017/12/26  18:40            98,132 pip3.6.exe
2017/12/26  18:40            98,132 pip3.exe
               5 個檔案        490,716 位元組
               2 個目錄 76,675,727,360 可用位元組

D:\Python\Python36\Scripts>pip install requests
Collecting requests
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    46% |██████████████▊                 | 40kB 22kB/s eta 0:00:0
    57% |██████████████████▌             | 51kB 16kB/s eta 0:
    69% |██████████████████████▏         | 61kB 20kB/s et
    80% |█████████████████████████▉      | 71kB 18kB/s
    92% |█████████████████████████████▌  | 81kB 21
    100% |████████████████████████████████| 92kB
 21kB/s
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    38% |████████████▍                   | 51kB 7.3kB/s eta 0:00:12
    46% |██████████████▉                 | 61kB 8.7kB/s eta 0:00:
    54% |█████████████████▍              | 71kB 9.8kB/s eta 0:
    61% |███████████████████▉            | 81kB 10kB/s eta 0
    69% |██████████████████████▎         | 92kB 10kB/s et
    77% |████████████████████████▊       | 102kB 9.9kB/
    85% |███████████████████████████▎    | 112kB 10k
    92% |█████████████████████████████▊  | 122kB 1
    100% |████████████████████████████████| 133k
B 6.3kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    46% |██████████████▊                 | 61kB 12kB/s eta 0:00:0
    53% |█████████████████▏              | 71kB 11kB/s eta 0:0
    61% |███████████████████▋            | 81kB 12kB/s eta 0
    69% |██████████████████████▏         | 92kB 13kB/s et
    76% |████████████████████████▋       | 102kB 11kB/s
    84% |███████████████████████████     | 112kB 11kB
    92% |█████████████████████████████▌  | 122kB 1
    99% |████████████████████████████████| 133kB
    100% |████████████████████████████████| 143k
B 14kB/s
Collecting idna<2.7,>=2.5 (from requests)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    54% |█████████████████▍              | 30kB 16kB/s eta 0:0
    72% |███████████████████████▏        | 40kB 8.3kB/s
    90% |█████████████████████████████   | 51kB 10k
    100% |████████████████████████████████| 61kB
 12kB/s
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2018.1.18-py2.py3-none-any.whl (151kB)
    40% |█████████████                   | 61kB 8.7kB/s eta 0:00:11
    47% |███████████████▏                | 71kB 7.9kB/s eta 0:00
    54% |█████████████████▎              | 81kB 9.0kB/s eta 0:
    60% |███████████████████▌            | 92kB 8.7kB/s eta
    67% |█████████████████████▋          | 102kB 9.2kB/s e
    74% |███████████████████████▊        | 112kB 8.5kB/s
    81% |██████████████████████████      | 122kB 8.9kB
    87% |████████████████████████████    | 133kB 7.9
    94% |██████████████████████████████▎ | 143kB
    100% |████████████████████████████████| 153k
B 12kB/s
Installing collected packages: urllib3, chardet, idna, certifi, requests
Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 requests-2.18.4
urllib3-1.22

D:\Python\Python36\Scripts>
下載後的位置,好像pip install會把第三庫依賴的庫,也幫你下載好。


2.如果用的是python2.7,執行如下程式碼塊會報錯:

D:\Python27\python.exe E:/MyCodes/Python27/20180130/Test.py
Traceback (most recent call last):
  File "E:/MyCodes/Python27/20180130/Test.py", line 9, in <module>
    file = open('taobao_test.txt','w',encoding='utf-8')
TypeError: 'encoding' is an invalid keyword argument for this function

後來忽然意識到是python版本的原因,看了python2.7.9和3.6.4的docs,發現2.7.9的open函式只有兩個引數,而3.6.4有三個引數。