2017-05-07 44 views
0

我已經嘗試了很多次,但它不工作:此代碼爲什麼只下載一個頁面的數據?

import requests 
from lxml import html, etree 
from selenium import webdriver 
import time, json 

#how many page do you want to scan 
page_numnotint = input("how many page do you want to scan") 
page_num = int(page_numnotint) 
file_name = 'jd_goods_data.json' 


url = 'https://list.jd.com/list.html?cat=1713,3264,3414&page=1&delivery=1&sort=sort_totalsales15_desc&trans=1&JL=4_10_0#J_main' 
driver = webdriver.Chrome() 
driver.get(url) 
base_html = driver.page_source 
selctor = etree.HTML(base_html) 
date_info = [] 
name_data, price_data = [], [] 
jd_goods_data = {} 
for q in range(page_num): 
    i = int(1) 
    while True: 
     name_string = '//*[@id="plist"]/ul/li[%d]/div/div[3]/a/em/text()' %(i) 
     price_string = '//*[@id="plist"]/ul/li[%d]/div/div[2]/strong[1]/i/text()' %(i) 
     if i == 60: 
      break 
     else: 
      i += 1 
     name = selctor.xpath(name_string)[0] 
     name_data.append(name) 
     price = selctor.xpath(price_string)[0] 
     price_data.append(price) 
     jd_goods_data[name] = price 

     print(name_data) 
     with open(file_name, 'w') as f: 
      json.dump(jd_goods_data, f) 
    time.sleep(2) 
    driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[1]/a[10]').click() 
    time.sleep(2) 

    # for k, v in jd_goods_data.items(): 
    #  print(k,v) 

我想下載一些細節,但它不工作。如果您輸入2進行掃描,它只會下載一個頁面的詳細信息,但會下載兩次!

+1

在使用你的變量'q'(在一個範圍內(在PAGE_NUM'分配給Q):'我想你設置它通過'輸入'函數到'2',但是如果你想從第二頁加載細節,你將不得不讓你的腳本成爲這個'q'的函數。 – Kanak

+0

我使用了一個變量'q'使範圍工作,然後使循環工作 –

回答

0

好吧,你定義q,但你並沒有真正使用它。在這種情況下,慣例是將這個未使用的變量命名爲_。我的意思是,與其做

for q in range(page_num): 

你應該做的

for _ in range(page_num): 

因此,其他編程人員將直接知道你不使用q,只希望重複您的操作。

這意味着(由於某些原因)行driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[1]/a[10]').click()不能正確執行。肯定有辦法讓它工作。但在你的情況下,我啓發式地看到你的url包含一個名爲page的參數。我建議你改用它。這因此導致實際使用的變量q因爲如此,如下:

import requests 
from lxml import html,etree 
from selenium import webdriver 
import time, json 

#how many page do you want to scan 
page_numnotint = input("how many page do you want to scan") 
page_num = int(page_numnotint) 
file_name = 'jd_goods_data.json' 

driver = webdriver.Chrome() 
date_info = [] 
name_data, price_data = [], [] 
jd_goods_data = {} 
for q in range(page_num): 
    url = 'https://list.jd.com/list.html?cat=1713,3264,3414&page={page}&delivery=1&sort=sort_totalsales15_desc&trans=1&JL=4_10_0#J_main'.format(page=q) 
    driver.get(url) 
    base_html = driver.page_source 
    selctor = etree.HTML(base_html) 
    i = 1 
    while True: 
     name_string = '//*[@id="plist"]/ul/li[%d]/div/div[3]/a/em/text()' %(i) 
     price_string = '//*[@id="plist"]/ul/li[%d]/div/div[2]/strong[1]/i/text()' %(i) 
     if i == 60: 
      break 
     else: 
      i += 1 
     name = selctor.xpath(name_string)[0] 
     name_data.append(name) 
     price = selctor.xpath(price_string)[0] 
     price_data.append(price) 
     jd_goods_data[name] = price 

     print(name_data) 

with open(file_name, 'w') as f: 
    json.dump(jd_goods_data, f) 

driver.quit() 
+0

你是對的,我可能會在狹窄,我只知道使用點擊進入下一頁,這是對我來說最好的教訓,無論如何(出於某種原因)本網站有一個小問題導致程序無法正常工作:頁面= 1和頁面= 0是JD.com.so中的同一網站。我必須添加q + = 1,非常感謝您解決我的程序,t帽子讓我在兩天內瘋狂!非常感謝你,即時中文,我不知道該如何深深地說:非常感謝你 –

+0

非常感謝你 –

+0

非常感謝你,即時新的這裏你能告訴我它工作嗎?(選擇你的答案是最好的) –

相關問題