twitter.py 889 B

123456789101112131415161718192021222324252627
  1. from urlparse import urljoin
  2. from urllib import urlencode
  3. from lxml import html
  4. from cgi import escape
  5. categories = ['social media']
  6. base_url = 'https://twitter.com/'
  7. search_url = base_url+'search?'
  8. def request(query, params):
  9. global search_url
  10. params['url'] = search_url + urlencode({'q': query})
  11. return params
  12. def response(resp):
  13. global base_url
  14. results = []
  15. dom = html.fromstring(resp.text)
  16. for tweet in dom.xpath('//li[@data-item-type="tweet"]'):
  17. link = tweet.xpath('.//small[@class="time"]//a')[0]
  18. url = urljoin(base_url, link.attrib.get('href'))
  19. title = ''.join(tweet.xpath('.//span[@class="username js-action-profile-name"]//text()'))
  20. content = escape(''.join(tweet.xpath('.//p[@class="js-tweet-text tweet-text"]//text()')))
  21. results.append({'url': url, 'title': title, 'content': content})
  22. return results