You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.2 KiB
37 lines
1.2 KiB
import html
|
|
|
|
import requests
|
|
|
|
from pyquery import PyQuery as pq
|
|
|
|
|
|
BASE_URL = 'https://www.cheese.com'
|
|
|
|
|
|
class CheeseComError(Exception):
|
|
pass
|
|
|
|
|
|
class CheeseCom(object):
|
|
def query(self, q):
|
|
try:
|
|
r = requests.get(BASE_URL, params=dict(q=q))
|
|
d = pq(r.content)
|
|
if not r.history:
|
|
url = d('div[class~="cheese-item"]').eq(0).find('a').eq(0).attr('href')
|
|
if not url:
|
|
raise CheeseComError('Not found')
|
|
r = requests.get(BASE_URL + url)
|
|
d = pq(r.content)
|
|
summary = []
|
|
for p in d('ul[class~="summary-points"]').find('p').items():
|
|
summary.append(html.unescape(p.html(method='text')))
|
|
return dict(
|
|
url=d('meta[name="twitter:url"]').attr('content'),
|
|
name=html.unescape(d('meta[name="twitter:title"]').attr('content')),
|
|
description=html.unescape(d('meta[name="twitter:description"]').attr('content')),
|
|
image=d('meta[name="twitter:image"]').attr('content'),
|
|
summary=summary)
|
|
except Exception as e:
|
|
raise CheeseComError(str(e))
|