資源簡介
本人進(jìn)行學(xué)校python課程設(shè)計的實戰(zhàn)成果,實現(xiàn)的整個過程詳見博客。此程序利用網(wǎng)絡(luò)爬蟲技術(shù)獲取平臺購物信息從而實現(xiàn)跨平臺比價功能以及相關(guān)擴展功能,難度適中,適合作為課設(shè)進(jìn)行開發(fā)。

代碼片段和文件信息
import?requests
import?re
import?time
import?os
from?bs4?import?BeautifulSoup?as?bs
from?selenium?import?webdriver
from?Goods?import?Goods
class?CrawlProgram:
????def?getHTMLText(selfurl):
????????try:
????????????kv?=?{‘user-agent‘:?‘Mozilla/5.0‘}
????????????r?=?requests.get(url?headers=kv?timeout=30)
????????????r.raise_for_status()
????????????r.encoding?=?r.apparent_encoding
????????????return?r.text
????????except:
????????????print(‘獲取HTML失敗‘)
????????????return?““
????def?getItemsFromTmall(selfkeywordnumber):
????????#?單個頁面最多獲取60個商品
????????url?=?‘https://list.tmall.com/search_product.htm?q=‘?+?keyword
????????soup?=?bs(self.getHTMLText(url)?‘html.parser‘)
????????try:
????????????#?get_title
????????????title_ls?=?[]
????????????for?item?in?soup.find_all(‘p‘?class_=‘producttitle‘):
????????????????if?item.find(‘a(chǎn)‘):
????????????????????title_ls.append(item.find(‘a(chǎn)‘).get(‘title‘))
????????????????else:
????????????????????title_ls.append(‘無相關(guān)信息‘)
????????????#?get_href?and?id
????????????href_ls?=?[]
????????????id_ls?=?[]
????????????reg?=?re.compile(r‘\d{515}‘)
????????????for?item?in?soup.find_all(‘p‘?class_=‘producttitle‘):
????????????????if?item.find(‘a(chǎn)‘):
????????????????????href?=?‘https:‘?+?item.find(‘a(chǎn)‘).get(‘href‘)
????????????????????href_ls.append(href)
????????????????????id?=?reg.findall(href)
????????????????????if?id:
????????????????????????id_ls.append(‘T‘+id[0])
????????????????????else:
????????????????????????id_ls.append(‘無相關(guān)信息‘)
????????????????else:
????????????????????href_ls.append(‘無相關(guān)信息‘)
????????????#?get_monthly_sales
????????????sale_ls?=?[]
????????????for?item?in?soup.find_all(‘p‘?class_=‘productStatus‘):
????????????????if?item.find(‘em‘):
????????????????????sale_ls.append(item.find(‘em‘).string[:-1])
????????????????else:
????????????????????sale_ls.append(‘無相關(guān)信息‘)
????????????#?get_shop_info
????????????shop_ls?=?[]
????????????for?item?in?soup.find_all(‘p‘?class_=‘productStatus‘):
????????????????if?item.find(‘span‘?class_=True):
????????????????????shop_ls.append(item.find(‘span‘?class_=True).get(‘data-tnick‘))
????????????????else:
????????????????????shop_ls.append(‘無相關(guān)信息‘)
????????????#?get_price
????????????price_ls?=?[]
????????????for?item?in?soup.find_all(‘p‘?class_=‘productPrice‘):
????????????????if?item.find(‘em‘):
????????????????????price_ls.append(item.find(‘em‘).get(‘title‘))
????????????????else:
????????????????????price_ls.append(‘無相關(guān)信息‘)
????????????items?=?[]
????????????for?num?in?range(len(title_ls)):
????????????????goods=?Goods(id_ls[num]‘天貓‘title_ls[num]shop_ls[num]eval(price_ls[num])sale_ls[num]href_ls[num])
????????????????items.append(goods)
????????????return?items[:number]
????????except:
????????????print(‘獲取天貓商品信息失敗‘)
????????????return?[]
????def?getItemsFromJD(selfkeywordnumber):
????????#?單個頁面最多獲取30個商品,銷量為月銷量
????????url?=?‘https://search.jd.com/Search?keyword=‘
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????9651??2020-06-12?12:42??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\CrawlProgram.py
?????文件????????3068??2020-06-12?09:23??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\DBConnection.py
?????文件?????3566280??2019-10-12?08:38??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\geckodriver.exe
?????文件????????2786??2020-06-11?13:31??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\Goods.py
?????文件????????1959??2020-06-10?21:21??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\GoodsList.py
?????文件???????12281??2020-06-11?18:30??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\Gui.py
?????文件????????1394??2020-06-12?09:26??python課設(shè)--網(wǎng)購平臺比價系統(tǒng)(天貓、京東、拼多多)\操作介紹.txt
評論
共有 條評論