Process finished with exit code 0 with no expected output showed

Viewed 329

So I was learning to write a really simple web scraping project, everything was okay until what I expected to see didn't show up, but exit code 0 so I assumed nothing went wrong with the code. Can anyone help me to see what's going wrong?

import requests

from bs4 import BeautifulSoup

headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15"}

res = requests.get('http://bj.xiaozhu.com/', headers=headers)  #get method of appending the request header
soup = BeautifulSoup(res.text, 'html.parser')
prices = soup.select('#page_list > ul > li > div.result_btm_con.lodgeunitname > span.result_price > I') #selector path of the prices 

for price in prices:
    print(price.get_text())

The output is supposed to print a list of prices from the website that I've scraped from, but instead Pycharm returned Process finished with exit code 0.

1 Answers

The CSS selector in your code is very specific and doesn't select the right tag. Try to change it to .result_price i:

import requests
from bs4 import BeautifulSoup

headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15"}

res = requests.get('http://bj.xiaozhu.com/', headers=headers)  #get method of appending the request header
soup = BeautifulSoup(res.text, 'html.parser')
prices = soup.select('.result_price i')

for price in prices:
    print(price.get_text())

Prints:

198
345
298
338
298
418
368
598
118
338
558
258
408
298
358
258
388
100
296
528
146
398
118
360
Related