![](/img/trans.png)
[英]I'm trying to use a Python web scraper to pull text from a page that has that text update periodically
[英]Python - I'm trying to create a simple web scraper for the steam market
所以我在学校里并且完成了Python入门课程后,我决定使用自己的技能尝试做一些有用的事情,所以我想编写一个脚本来刮除Steam市场网页并在列出某项商品时通知我价格等于或低于所需价格。 我有些困惑,希望我能得到任何提示以帮助我。 我正在使用urllib2和BeautifulSoup
from bs4 import BeautifulSoup
from urllib2 import urlopen
import time
item = str(raw_input('Please enter the item you are looking for(Exact URL): '))
price = str(raw_input('Please enter the price you want to buy the item at: '))
print('Searching for item at that price....\n' + item)
market = urlopen(item)
def getPrices(market,desiredPrice):
while True:
soup = BeautifulSoup(market)
prices = soup.findAll('span',{'class':'market_listing_price market_listing_price_with_fee'})
"""
So now my logic assumed I should do something like;
if desiredPrice in prices:
print('found item at the desired price!')
return link_to_item
"""
print('Searching...')
time.sleep(20)
getPrices(market, price)
为了进行测试,我使用了以下蒸汽市场链接: https : //steamcommunity.com/market/listings/730/AK-47%20%7C%20Redline%20%28Field-Tested%29
并且包含首页上每个项目价格的跨度为class ='market_listing_price market_listing_price_with_fee'
底线问题:
我似乎无法仅从每个span标签中获取数据; 我只想以浮动价格获取价格并将它们放入列表中,然后就可以对价格进行排序了。 然后,我可以将它们与所需价格进行比较,找到低于所需价格的任何商品。
这些跨度中有很多文字。 如果将其过滤掉,那应该没问题。
>>> [i.text.strip() for i in prices]
[u'Sold!', u'\xa5 33.69', u'\xa5 33.69', u'Sold!', u'\xa5 33.69', u'\xa5 33.69', u'\xa5 33.69', u'\xa5 33.69', u'\xa5 33.69', u'\xa5 33.69']
那里有日元符号,除非您需要货币信息,否则也可以将其取出。
要只获取数字,我会这样做:
prices = [i.text.strip() for i in prices]
prices = [float(k) for k in [''.join([j for j in i if j in '0123456789.']) for i in prices] if k]
if min(prices)< desiredPrice:
请记住,您需要先float(desiredPrice)
,并确保您正在循环中读取Web数据。 目前,您将每20秒检查一次完全相同的数据!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.