1. 結(jié)果如下:
2. 程序源碼:
```
#!/usr/bin/env python
# -*- coding: utf8 -*-
import requests
from bs4 import BeautifulSoup
def get_links_from(who_sells):
urls = []
url_base = "http://bj.58.com/pingbandiannao/{}/".format(str(who_sells))
r = requests.get(url_base)
soup = BeautifulSoup(r.text, 'lxml')
for url in soup.select('tr.zzinfo td.t? a.t '):
urls.append(url.get("href").split('?')[0])
# print(urls)
return urls
def download(who_sells):
# r = requests.get(url, proxies=proxies, headers=headers)
urly = get_links_from(who_sells)
for url1 in urly:
#print(url1)
wb_data = requests.get(url1)
soup = BeautifulSoup(wb_data.text, 'lxml')
title = soup.title.text.strip()
price = soup.select('span.price_now i')
place = soup.select('div.palce_li i')
domain = soup.select('span.crb_i a')
views = soup.select('span.look_time')
# pr)int (views
data = {
'domain': domain[0].text,
'title': title,
'price': price[0].text,
'place': place[0].text,
'views': views[0].text,
'var' : '個(gè)人' if who_sells == 0 else '公司'
}
print (data)
def main():
download(0)
# get_links_from(who_sells=1)
if __name__ == '__main__':
main()
```
3. 經(jīng)驗(yàn)總結(jié):方式
- 學(xué)習(xí)了用STRIP()去除回車符號(hào)
-學(xué)習(xí)了主函數(shù)的調(diào)用次序
- 學(xué)習(xí)了中文字符串的顯示