2017-06-21 92 views
0

我正在嘗試改編一個python 2.7 craigslist scraper,我在網上發現使用python 3.6。改編Craigslist Scraper Python

但每次運行python腳本時它都不會返回任何內容。 是因爲我沒有針對正確的html標籤?如果是這樣,我將如何定位正確的html標籤?

我假設它是這裏的這部分代碼:

for listing in soup.find_all('p',{'class':'result-row'}): 
    if listing.find('span',{'class':'result-price'}) != None: 

完全腳本如下。

先謝謝您。

import requests 
from bs4 import BeautifulSoup 
from urllib.parse import urljoin 

URL = 'https://vancouver.craigslist.ca/search/sss?query=Vespa' 
BASE = 'https://vancouver.craigslist.ca/' 

response = requests.get(URL) 

soup = BeautifulSoup(response.content,"html.parser") 
for listing in soup.find_all('p',{'class':'result-row'}): 
    if listing.find('span',{'class':'result-price'}) != None: 
     price = listing.text[2:6] 
     price = int(price) 
     if price <=3600 and price > 1000: 
      print (listing.text) 
      link_end = listing.a['href'] 
      url = urljoin(BASE, link_end) 
      print (url) 
      print ("\n") 
print('test') 

回答

0

你說得對這個是可能的問題:

for listing in soup.find_all('p',{'class':'result-row'}): 
    if listing.find('span',{'class':'result-price'}) != None: 

這件作品有被編輯爲你刮特定網頁。你看過頁面的HTML並驗證了這兩行嗎?如果沒有,請右鍵單擊頁面並選擇「查看頁面源」。然後你必須找到你想要的特定數據。

如果我想從一個網頁看起來像這樣在HTML搶東西:

<div class='what'>hello</div> 

我會改變上面這個代碼:

for listing in soup.find_all('div',{'class':'what'}): 
    # do something