twitter.py 873 B

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