- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
它显示公寓的最高价格为 4700 美元,而我看到的最高价格超过 100 万美元。为什么它没有显示这一点?我做错了什么?
import requests
import re
r = requests.get("http://orlando.craigslist.org/search/apa")
r.raise_for_status()
html = r.text
matches = re.findall(r'<span class="price">\$(\d+)</span>', html)
prices = map(int, matches)
print "Highest price: ${}".format(max(prices))
print "Lowest price: ${}".format(min(prices))
print "Average price: ${}".format(sum(prices)/len(prices))
最佳答案
使用 html 解析器 bs4非常容易使用,您可以通过在网址中添加 ?sort=pricedsc
按价格排序,这样第一个匹配项将是最大值,最后一个匹配项将是最后一个最低项(对于该页面):
r = requests.get("http://orlando.craigslist.org/search/apa?sort=pricedsc")
from bs4 import BeautifulSoup
html = r.content
soup = BeautifulSoup(html)
print "Highest price: ${}".format(prices[0])
print "Lowest price: ${}".format(prices[-1])
print "Average price: ${}".format(sum(prices, 0.0)/len(prices))
如果您想要最低价格,则需要按升序排列:
r = requests.get("http://orlando.craigslist.org/search/apa?sort=priceasc")
from bs4 import BeautifulSoup
html = r.content
soup = BeautifulSoup(html)
prices = [int(pr.text.strip("$")) for pr in soup.select("span.price")]
print "Highest price: ${}".format(prices[-1])
print "Lowest price: ${}".format(prices[0])
print "Average price: ${}".format(sum(prices, 0.0)/len(prices))
现在输出非常不同:
Highest price: $70
Lowest price: $1
Average price: $34.89
如果你想要所有的平均值,你需要添加更多的逻辑。默认情况下,您只能看到 2500 个结果中的 100 个
结果,但我们可以更改这一点。
r = requests.get("http://orlando.craigslist.org/search/apa")
from bs4 import BeautifulSoup
html = r.content
soup = BeautifulSoup(html)
prices = [int(pr.text.strip("$")) for pr in soup.select("span.price")]
# link to next 100 results
nxt = soup.select_one("a.button.next")["href"]
# keep looping until we find a page with no next button
while nxt:
url = "http://orlando.craigslist.org{}".format(nxt)
r = requests.get(url)
soup = BeautifulSoup(r.content)
# extend prices to our list
prices.extend([int(pr.text.strip("$")) for pr in soup.select("span.price")])
nxt = soup.select_one("a.button.next")
if nxt:
nxt = nxt["href"]
这将为您提供 1-2500
的每个列表
关于python - 网络抓取 python 中的 craigslist 公寓价格未显示最高成本公寓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679285/
我很难理解进程外环境中的COM公寓。 基本上我不明白为什么客户端需要调用CoInitializeEx在公寓中注册自己的线程。 我可以理解STA或MTA中的服务器线程和服务器线程使用的对象。但是我不明白
关联(belongs_to)模型出现以下错误: PG::ForeignKeyViolation: ERROR: insert or update on table "employees" violat
我正在使用 apartment gem 和 MySQL 作为数据库在 Rails 中创建一个 Multi-Tenancy 应用程序。我在https://gorails.com/episodes/mul
我在尝试时遇到错误 bundle exec rails generate apartment:install 得到这个错误, build': undefined method new' for "Ap
我在尝试在我的 Rails 5.1 项目上设置 Gitlab CI 时遇到一个奇怪的问题,该项目使用 apartment gem 来支持 Multi-Tenancy 。我已经设置了 docker-co
我知道默认情况下,公寓会在公共(public)数据库和租户数据库上创建相同的表。而且公共(public)数据库上的大多数表都是没有用的,空表。我也知道租户数据库依赖于 schema.rb 但我想知道我
我正在使用这个 apartment ruby 。 我在 application.rb 文件中添加了这个: config.middleware.use 'Apartment::Elevators::Su
在执行 rake db:migrate 时,出现此错误: Migrating alpha tenant One of the following schema(s) is invalid: "alph
根据 railscast,我当前的设置是一个 Multi-Tenancy 应用程序 389-multitenancy-with-postgresql 我正在尝试将设置移动到使用 apartment g
我正在使用 heroku 和 Rails 构建一个 SAAS 平台。为此,我使用 apartment gem 管理租户。 在我的本地开发中这没有问题,但是当我尝试使用到 heroku 时出现下一个错误
我是一名优秀的程序员,十分优秀!